0

I am trying to learn PyQt to develop a GUI for viewing images. I found some code that looked like It could be easily converted to fit my use case. Figured going line by line to check the logic would help me understand PyQt.

I am getting stuck at this line in the following class

from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QFileDialog

class Ui_MainWindow(QtWidgets.QMainWindow):
    def __init__(self, imgs_path,masks_path, pred_path, study):
        super(Ui_MainWindow, self).__init__()
        # sets up ui
        self.setObjectName("MainWindow")
        self.resize(1440, 1000)
        self.centralwidget = QtWidgets.QWidget()
        self.centralwidget.setObjectName("centralwidget")
        ....
        self.setCentralWidget(self.centralwidget)
      

The line which goes self.setCentralWidget(self.centralwidget), what exactly is being set as the central widget? I tried to look at the documentation but wasn't able to understand what was being set as the central widget. It looks like QtWidgets.QtWidget() is just a class containing methods for user interface?

The tutorial I was following introduced self.centralwidget in the context of setting it to some type of widget like QtWidgets.Qlabel() etc, so I am not sure what is actually being set as the central widget in this case.

visual360
  • 5
  • 2
  • 2
    `QWidget` is just a generic widget, with no predefined behavior. You could give it content by adding child widgets, or by handling paint events to draw something into the widget. – jasonharper Mar 31 '21 at 16:01
  • I would suggest trying qtdesigner. It can help you figure out what are you creating (in my case it did). Btw you can imagine QWidget as an empty frame where you can insert other objects like labels button etc.. https://build-system.fman.io/qt-designer-download – Giovanni Frison Mar 31 '21 at 16:06
  • I voted to reopen, as the question is not only about the role of a central widget, but what a QWidget is *and* its usage for the central widget. – musicamante Mar 31 '21 at 17:51

1 Answers1

0

I'll try to explain it to you visually:

enter image description here

In the picture, you can see the main frame, your QtWidgets.QMainWindow which, in this case, is the parent container to all the objects you'll place in your UI. Then you have that highlighted rectangle in the middle, which is actually a QtWidgets.QWidget(). The fact that you are setting it as Centralwidget I think is more of a positional constrain/indication. In turn, QtWidgets.QWidget() can contains other elements (even another QtWidgets.QWidget() for example).

Giovanni Frison
  • 628
  • 3
  • 19
  • 1
    Note that in Designer the central widget is created by default (and cannot be changed, if not by promotion), so what you're showing is actually a widget that is child of the central widget; having a central widget is not just a choice, but almost a requirement (in fact, Qt docs suggests that *not* setting a central widget for QMainWindow is not supported) – musicamante Mar 31 '21 at 17:21
  • @musicamante yes you are right. I thought his main doubt was more about what a Qwidget is in concrete. This seems a good way to understand it imo. – Giovanni Frison Apr 01 '21 at 07:19
  • Seeing it in this way is indeed useful. So you're basically setting a blank widget as the central widget, because its a requirement to have one. I didnt know that, thanks – visual360 Apr 01 '21 at 09:43
  • @visual360 happy to help! happier if you upvote or accept ;D – Giovanni Frison Apr 01 '21 at 09:52
  • 1
    Done, altho it's strange I can't publicly upvote on my own question... – visual360 Apr 01 '21 at 10:04
  • @visual360 some reputation is required to gain certain authorizations, but in anycase upvoting your own question is almost pointless. Besides that, there are other reasons behind setting the central widget, unfortunately your question was closed just seconds before I posted my answer that covered those aspects too. If you get enough attention, maybe it will be opened again. – musicamante Apr 01 '21 at 13:12
  • I didnt read back what i typed... I meant to say its 'strange i cant publicly upvote the ANSWER to my own question'. I understand the point about reputation. Can you not send the other reasons via comments? Or are the other reasons too long for comment format – visual360 Apr 01 '21 at 13:50