Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Basic QT5 Help



  • Can someone please help me understand why this code isn't running? I am very new to QT5. I get the following error messages (I am running in pycharm):
    Traceback (most recent call last):
    File "/Users/oliverm/PycharmProjects/Plural-Sight /Basic Structure/1.py", line 26, in <module>
    dialog = HelloWorld()
    File "/Users/oliverm/PycharmProjects/Plural-Sight /Basic Structure/1.py", line 18, in init
    layout.addWidget(label)
    TypeError: addWidget(self, QWidget, stretch: int = 0, alignment: Union[Qt.Alignment, Qt.AlignmentFlag] = 0): first argument of unbound method must have type 'QBoxLayout'
    Code:
    from PyQt5.QtCore import *
    from PyQt5.QtGui import *
    from PyQt5.QtWidgets import *
    from PyQt5.Qt import *
    import sys

    class HelloWorld(QDialog):
    def init(self):
    QDialog.init(self)

        layout = QVBoxLayout
    
        label = QLabel
        line_edit = QLineEdit()
        button = QPushButton("Close")
    
        layout.addWidget(label)
        layout.addWidget(line_edit)
        layout.addWidget(button)
    
        self.setLayout(layout)
    

    app = QApplication(sys.argv)
    dialog = HelloWorld()
    dialog.show()
    app.exec_()



  • @Olivermadsen

        layout = QVBoxLayout
    
        label = QLabel
    

    I use PyQt/Python. Unless I am wildly mistaken, you can't write those, this is not C++! I would assume those set the label to the function, they do not call the function.

        layout = QVBoxLayout()
    
        label = QLabel()
    

    Does that remove your errors? You cannot lazily omit the () like you can in C++ (layout = new QVBoxLayout, which I don't much like anyway), put them in always.



  • @Olivermadsen

        layout = QVBoxLayout
    
        label = QLabel
    

    I use PyQt/Python. Unless I am wildly mistaken, you can't write those, this is not C++! I would assume those set the label to the function, they do not call the function.

        layout = QVBoxLayout()
    
        label = QLabel()
    

    Does that remove your errors? You cannot lazily omit the () like you can in C++ (layout = new QVBoxLayout, which I don't much like anyway), put them in always.



  • @JonB Thanks Jon! works perfectly.


Log in to reply