0

I am implementing an app through a class that has a QMainWindow member. I call two functions (create_side_menu, create_mixing_window) that display dynamically added widgets inside a bigger widget, which is added to the QMainWindow. The first function works, while the second does not, even though very similar to the first. Can you help me solving this problem?

Update: I modified the code as it appears now in the question, and debugging it seems that the problem is at line 78 (wid = ChannelModifier(af)) where it throws the error: "QWidget: Must construct a QApplication before a QWidget" even though the application is defined in MainWindow class init

Notes:

  • ChannelModifier is a custom class deriving from QWidget

      class MainWindow:
          def __init__(self):
              self.QListWidgetLeft = None  # QListWidget
              self.QListWidgetRight = None  # QListWidget
              self.central_widget = None
              self.manager = Manager.get_instance()
              self.app = QApplication(sys.argv)
              self.window = QMainWindow()
              self.set_window()
              self.set_menu_bar()
              self.sb = self.window.statusBar()
              self.sb.showMessage(f"ready to remix")
              self.window.show()
              sys.exit(self.app.exec())
    
          def set_window(self):
              # self.window.setFixedSize(1080, 720)
              self.window.setGeometry(50, 50, 1500, 1000)
              self.window.setWindowIcon(QIcon("assets/icon.png"))
              # self.window.setStyleSheet('background-color: #b7dfeb')
              self.window.setStyleSheet('background-color:qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:1, stop:0 '
                                        'rgba(165, 165, 247, 255), stop:1 rgba(255, 255, 255, 255))')
              self.window.setWindowTitle("Music Remixes and Mashups")
              self.create_central_widget()
    
          def create_central_widget(self):
              self.central_widget = QWidget()
              self.window.setCentralWidget(self.central_widget)
              self.central_widget.setLayout(QHBoxLayout())
              self.create_side_menu()
              self.create_mixing_window()
    
          def create_side_menu(self):
              widget = QWidget()
              vbox = QVBoxLayout()
              scrollbar = QScrollBar()
              scrollbar.setMaximum(100)
              scrollbar.setStyleSheet("background-color: rgb(60,60,90); width: 14px; border-radius 0px;")
              scrollbar.sliderMoved.connect(scrollbar.value)
              self.QListWidgetLeft = QListWidget()
              self.QListWidgetLeft.setSpacing(5)
              self.QListWidgetLeft.setVerticalScrollBar(scrollbar)
              self.QListWidgetLeft.setVerticalScrollBarPolicy(Qt.ScrollBarPolicy.ScrollBarAlwaysOn)
    
              pr = self.manager.get_current_project()
              if pr:
                  for af in pr.get_audio_files():
                      self.add_audiofile_to_side_menu(af)
              vbox.addWidget(self.QListWidgetLeft)
              widget.setLayout(vbox)
              widget.setFixedWidth(230)
              widget.setStyleSheet('background-color:white')
              self.central_widget.layout().addWidget(widget)
    
          def add_audiofile_to_side_menu(self, af: AudioFile):
              if self.QListWidgetLeft is None:
                  self.QListWidgetLeft = QListWidget()
              label = QLabel()
              label.setFixedWidth(180)
              label.setFixedHeight(180)
              if af.get_thumb_path():
                  label.setPixmap(QPixmap(af.get_thumb_path()))
              else:
                  label.setText(af.get_title())
              item = QListWidgetItem(self.QListWidgetLeft)
              item.setSizeHint(label.size())
              self.QListWidgetLeft.addItem(item)
              self.QListWidgetLeft.setItemWidget(item, label)
    
          def create_mixing_window(self):  # TODO: it doesn't work
              mixing_window = QWidget()
              grid = QGridLayout()
    
              pr = self.manager.get_current_project()
              if pr:
                  for af in pr.get_audio_files():
                      # self.add_channel_modifier_to_mixing_menu(af)
                      wid = ChannelModifier(af)
                      grid.addWidget(wid)
              # grid.addWidget(self.QListWidgetRight)
              mixing_window.setLayout(grid)
              mixing_window.setStyleSheet('background-color:white')
              self.central_widget.layout().addWidget(mixing_window)
    
          def add_channel_modifier_to_mixing_menu(self, af: AudioFile):
              if self.QListWidgetRight is None:
                  self.QListWidgetRight = QListWidget()
              wid = ChannelModifier(af)
              item = QListWidgetItem(self.QListWidgetRight)
              # item.setSizeHint(wid.size())
              self.QListWidgetRight.addItem(item)
              self.QListWidgetRight.setItemWidget(item, wid)
    
M04
  • 31
  • 1
  • 1
  • 4
  • It is interesting that you provide some code that we can try to reproduce the problem. Looking at line 7, where is Manager declared? Looking at lines 56 and 97, where is AudioFile declared? – Jonas Vieira de Souza Jul 08 '22 at 09:43
  • Manager and Audiofile are two classes that I defined in other python files and that I imported. Manager is a manager for various audio remixes projects that contain AudioFiles. – M04 Jul 08 '22 at 13:10

1 Answers1

0

With the limited amount of code you have included it is difficult to say if this is what is causing your problem or not, but one issue I see is that you aren't using the correct signature in your calls to addWidget() on your grid layout.

With grid layout you have to specify the column and row you want the widget to occupy when adding them to the layout.

def create_mixing_window(self):  # TODO: it doesn't work
    mixing_window = QWidget()
    grid = QGridLayout()
    pr = self.manager.get_current_project()
    if pr:
        for af in pr.get_audio_files():
            self.add_channel_modifier_to_mixing_menu(af)
            wid = ChannelModifier(af)
            grid.addWidget(wid,0,0)  # row 0; column 0
            grid.addWidget(self.QListWidgetRight, 0, 1)  # row 0; column 1
    mixing_window.setLayout(grid)
    mixing_window.setStyleSheet('background-color:white')
    self.central_widget.layout().addWidget(mixing_window)
Alexander
  • 16,091
  • 5
  • 13
  • 29