Ismael Santana
Ismael Santana

Reputation: 11

Promote a QWidget to a QAxWidget using Qt Designer and PyQt5

I want to promote a QWidget to a QAxWidget. I want to avoid using QWebengineView to display a PDF file (I can't zoom it) so I decide to implement this solution instead (bottom solution): Solution

In there he coded the widget in the main window but I want to use Qt Designer to generate the .ui file. Following this solution solution using Qt Designer I can promote a QWidget to another class (as I understand promoting is subclassing a class) but I can't make the solution works for me. I have tried using QAxWidget and QAxContainer as header file but it's still not working. There is no doubt that I am not understanding promoting in Qt Designer.

This is my Qt Designer window:

what I do in Qt Designer to promote

This is my .ui file:

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>Form</class>
 <widget class="QWidget" name="Form">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>993</width>
    <height>758</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>Form</string>
  </property>
  <layout class="QHBoxLayout" name="horizontalLayout">
   <item>
    <widget class="pdfViewer" name="widget" native="true"/>
   </item>
  </layout>
 </widget>
 <customwidgets>
  <customwidget>
   <class>pdfViewer</class>
   <extends>QWidget</extends>
   <header location="global">QAxWidget</header>
   <container>1</container>
  </customwidget>
 </customwidgets>
 <resources/>
 <connections/>
</ui>

and this is my .py module:

from PyQt5 import QtCore, QtGui, QtWidgets


class Ui_Form(object):
    def setupUi(self, Form):
        Form.setObjectName("Form")
        Form.resize(993, 758)
        self.horizontalLayout = QtWidgets.QHBoxLayout(Form)
        self.horizontalLayout.setObjectName("horizontalLayout")
        self.widget = pdfViewer(Form)
        self.widget.setObjectName("widget")
        self.horizontalLayout.addWidget(self.widget)

        self.retranslateUi(Form)
        QtCore.QMetaObject.connectSlotsByName(Form)

    def retranslateUi(self, Form):
        _translate = QtCore.QCoreApplication.translate
        Form.setWindowTitle(_translate("Form", "Form"))
from QAxWidget import pdfViewer

The last line points an error: QAxWidget is not a package, is a class.

This is my mainwindow code:

from PyQt5 import QtCore, QtGui, QtWidgets
from view.container import Ui_Form
import sys
import os
import urllib.parse

class MainWindow(QtWidgets.QMainWindow, Ui_Form):
    def __init__(self):
        super().__init__()

        self.widget.setFocusPolicy(QtCore.Qt.StrongFocus)
        self.widget.setControl("{8856F961-340A-11D0-A96B-00C04FD705A2}")

        pdf_path = r"C:\Users\isDev\Documents\ismael\respaldo\UrimareGUI\ReportLab\datasheet.pdf"
        uri = urllib.parse.urljoin('file:', urllib.request.pathname2url(pdf_path))

        self.widget.dynamicCall('Navigate(const QString&)', uri)

if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    window = MainWindow()
    window.show()
    app.exec_()

and this is the error I get when I run the test.py:

Traceback (most recent call last):
  File "C:\Users\isDev\OneDrive\Documents\PythonScripts\urimareApp\test.py", line 2, in <module>
    from view.container import Ui_Form
  File "C:\Users\isDev\OneDrive\Documents\PythonScripts\urimareApp\view\container.py", line 30, in <module>
    from QAxWidget import pdfViewer
ModuleNotFoundError: No module named 'QAxWidget'

I don't know how to promote the QWidget in Qt Designer

Upvotes: 0

Views: 98

Answers (0)

Related Questions