Can't call another class

paparucino paparucino at gmail.com
Fri Sep 10 12:58:41 BST 2021


On 9/10/21 6:59 AM, Maurizio Berti wrote:

Thank you very much for your code, I will never forget your effort to 
help me.

While you lost your time (friendly) I tried to put togheter a simple 
piece of code to show what I would like to obtain. Of course it doesnt 
work mainly because I miss how to handle UI classes. I would like to 
read your opinion

Lets go:

https://i.imgur.com/2URinRF.jpg   Here you will see where the scroll 
class should show the result of the query. (self.stripTable = 
QtWidgets.QTableWidget(self.Strip_tab))

##### setup.py #####

from PyQt5.QtWidgets import QApplication, QWidget, QPushButton
from strips import *

class App(QWidget):
     def __init__(self):
         super().__init__()
         self.initUI()

     def initUI(self):
         self.setGeometry(10, 10, 320, 300)
         button = QPushButton('Select', self)
         button.move(100, 70)
         button.clicked.connect(self.on_click)
         self.show()
     def on_click(self):
         anno = '2021'
         mese = '9'
         cursor = 'xx'
         self.stripTable = QtWidgets.QTableWidget(self.Strip_tab)
         '''
         This should be added since the result of SomeWindow() should be 
shown into
         a tab of the project, otherwise in a popup menu
         '''
         SomeWindow.createTable(self, anno, mese, cursor)
         #SomeWindow.createTable(self, self.self.stripTable, anno, mese, 
cursor)

if __name__ == '__main__':
     app = QApplication(sys.argv)
     ex = App()
     sys.exit(app.exec_())

###### strips.py #####

import sys
import calendar

#from functions import *
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QMainWindow, QTableWidget
from single import Strip_MainWindow

class SpecialStyledItemDelegate(QtWidgets.QStyledItemDelegate):
     def __init__(self, parent=None):
         super().__init__(parent)
         self._values = dict()

     def add_text(self, text, row):
         self._values[row] = text

     def initStyleOption(self, option, index):
         super().initStyleOption(option, index)
         row = index.row()
         if row in self._values:
             option.text = self._values[row]
             option.displayAlignment = QtCore.Qt.AlignCenter

class SomeWindow(QMainWindow, Strip_MainWindow):
     def __init__(self, anno, month, cursor):
         super().__init__()
         self.setupUiStrip(self)

     def createTable(self, anno, mese, cursor): #I will need them in 
production
         zz = 'table_'
         cc = 0
         for aa in range(1, 13):
             self.vol_name = 'JOHN DOE'
             self.alphamese = 'Twisted'
             # table ='zz%s'%(str(cc))
             self.table = QTableWidget(self.scrollAreaWidgetContents)
             self.table.setGeometry(QtCore.QRect(0, 5, 150, 597))
             sizePolicy = 
QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Fixed, 
QtWidgets.QSizePolicy.Expanding)
             self.table.setSizePolicy(sizePolicy)
self.table.setSizeAdjustPolicy(self.table.AdjustToContents)
             self.table.setObjectName("StripTable")
             self.table.setRowCount(33)
             self.table.setColumnCount(3)
             self.special_delegate = SpecialStyledItemDelegate()
             self.table.setItemDelegate(self.special_delegate)
             h_header = self.table.horizontalHeader()
             h_header.hide()
             for i in range(h_header.count() - 1):
                 h_header.setSectionResizeMode(i, 
QtWidgets.QHeaderView.ResizeToContents)
             h_header.setSectionResizeMode(2, QtWidgets.QHeaderView.Fixed)
             v_header = self.table.verticalHeader()
             v_header.hide()
             v_header.setDefaultSectionSize(17)
             self.table.setSpan(1, 0, 1, 3)
             self.table.setSpan(0, 0, 1, 3)
             zx = 1
             self.special_delegate.add_text(self.vol_name, 0)
             self.special_delegate.add_text(self.alphamese, 1)
             for zx in range(1, 10):
                 dow = "do{}".format(zx)
                 value = "AAAAAAAA{}".format(zx)
                 self.table.setItem(zx + 1, 0, 
QtWidgets.QTableWidgetItem(str(zx)))
                 self.table.setItem(zx + 1, 1, 
QtWidgets.QTableWidgetItem(dow))
                 item = QtWidgets.QTableWidgetItem(value)
                 item.setTextAlignment(QtCore.Qt.AlignCenter)
                 self.table.setItem(zx + 1, 2, item)
             self.layout.addWidget(self.table)

'''
app = QApplication(sys.argv)
window = Ui_MainWindow()
window.show()
app.exec()
'''

##### single.py #####

from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QTableWidget


class SpecialStyledItemDelegate(QtWidgets.QStyledItemDelegate):
     def __init__(self, parent=None):
         super().__init__(parent)
         self._values = dict()

     def add_text(self, text, row):
         self._values[row] = text

     def initStyleOption(self, option, index):
         super().initStyleOption(option, index)
         row = index.row()
         if row in self._values:
             option.text = self._values[row]
             option.displayAlignment = QtCore.Qt.AlignCenter

class Strip_MainWindow(object):
     def __init__(self):
         super().__init__()
         self.setupUi(self,)

     def setupUiStrip(self, MainWindow,anno,mese,cursor):
         MainWindow.setObjectName("MainWindow")
         MainWindow.resize(800, 600)
         self.centralwidget = QtWidgets.QWidget(MainWindow)
         self.centralwidget.setObjectName("centralwidget")
         self.horizontalLayout = QtWidgets.QHBoxLayout(self.centralwidget)
         self.horizontalLayout.setObjectName("horizontalLayout")
         self.scrollArea = QtWidgets.QScrollArea(self.centralwidget)
         self.scrollArea.setWidgetResizable(True)
         self.scrollArea.setObjectName("scrollArea")
         self.scrollAreaWidgetContents = QtWidgets.QWidget()
         self.scrollAreaWidgetContents.setGeometry(QtCore.QRect(0, 0, 
780, 539))
self.scrollAreaWidgetContents.setObjectName("scrollAreaWidgetContents")
         self.layout = QtWidgets.QHBoxLayout(self.scrollAreaWidgetContents)
         self.layout.setContentsMargins(0, 0, 0, 0)
         self.layout.setObjectName("layout")
         #self.layout.addWidget(self.table)
         self.scrollArea.setWidget(self.scrollAreaWidgetContents)
         self.horizontalLayout.addWidget(self.scrollArea)
         MainWindow.setCentralWidget(self.centralwidget)
         self.menubar = QtWidgets.QMenuBar(MainWindow)
         self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 19))
         self.menubar.setObjectName("menubar")
         MainWindow.setMenuBar(self.menubar)
         self.statusbar = QtWidgets.QStatusBar(MainWindow)
         self.statusbar.setObjectName("statusbar")
         MainWindow.setStatusBar(self.statusbar)
         self.retranslateUi(MainWindow)
         QtCore.QMetaObject.connectSlotsByName(MainWindow)

     def retranslateUi(self, MainWindow):
         _translate = QtCore.QCoreApplication.translate
         MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))

> Il giorno gio 9 set 2021 alle ore 14:02 paparucino 
> <paparucino at gmail.com <mailto:paparucino at gmail.com>> ha scritto:
>
>     [...]
>     That link only shows instructions on how to prepare a good ssce but I
>     don't see any references to a mailing list
>
>
> What Florian meant was that you should use the instructions on that 
> website and then post to *this* mailing list.
>
>
> Il giorno mer 8 set 2021 alle ore 16:47 paparucino 
> <paparucino at gmail.com <mailto:paparucino at gmail.com>> ha scritto:
>
>     I never edit the ui file. I keep it as a base and eventually I
>     create many .py files depending on how the debug goes. I know that
>     it is confusing and that I fill the dir with files but sometimes I
>     start from an idea and as everything evolves I can change my mind
>     and therefore I have a base plus a certain series of more or less
>     functional back ups
>
> Creating multiple UI files for the same interface is not an issue, as 
> long as it's only done for UX purposes (aesthetic or design).
> I've prepared a basic example of what you might want to achieve. 
> There's a basic QMainWindow with an *empty* scroll area (but an 
> existing layout), and the code that allows you to create as many 
> tables as you want.
>
Pls, don't hate me. I tried your code but it doesnt solve my problem. I 
already obtained that, let me call this way, scrollArea via designer. My 
problem was/is to pass variables from another class (see setup.py) to 
the scrollArea, then show the result into the strip tab (see picture)

Forgive me

paparucino



-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://www.riverbankcomputing.com/pipermail/pyqt/attachments/20210910/0b8d7c54/attachment.htm>


More information about the PyQt mailing list