PyQt allign checkbox and put it in every row

前端 未结 2 449
清歌不尽
清歌不尽 2021-01-16 11:48

I\'m trying to do this with the check-box. Sadly is made for C++ and any adaptation of the code for Python has the result this error: \'QWidget\' object is not callabl

2条回答
  •  星月不相逢
    2021-01-16 12:15

    You could consider using the Qt Designer, so that you can:

    • Get your desired layout visually with immediate feedback
    • Get to see the actual code generated from it and spot what you were missing

    After you get your desired window (i.e. the file with a .ui extension), you can use the pyuic5[1] utility, which will generate a Python file from the UI file. From the man page

    pyuic5 - compile Qt5 user interfaces to Python code

    Steps

    Simple steps with example

    Create and Save the UI

    You should use the Qt Designer and save the .ui file.

    Generate Python code from the UI file

    If your .ui file is named mainwindow.ui then you can use the command:

    pyuic5 mainwindow.ui -o mainwindow.py
    

    Update your Python code

    Get your Python code to use the generated Python-based UI file.

    from PyQt5.QtWidgets import QMainWindow
    
    from mainwindow import Ui_MainWindow   # <<--- important
    
    # Set up the user interface from Designer.
    win = QMainWindow()
    gui = Ui_MainWindow()
    gui.setupUi(win)
    

    As you can see above, you need to import the class representing the UI from the Python module that was generated, in our case being the mainwindow.py file from the command in the prev. step.

    The class is automatically prefixed with Ui_ by the utility. You then instantiate a QMainWindow and the generated class and use Qt's built-in method setupUi for it to incorporate all the widgets, etc.

    Important: Every time you update your window in Qt Designer, you'll need to repeat step #2. Considering the amount of time saved by using the Designer directly, this shouldn't be a problem.

    Note: You can use the generated mainwindow.py file to read the code and see how the desired layout was achieved. This should be useful if you really do not want to continue using this approach.

    [1] The pyuic5 command is found within the pyqt5-dev-tools package, so a sudo apt-get install pyqt5-dev-tools should do it.

提交回复
热议问题