Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct

    Access MainWindow from different class

    General and Desktop
    2
    2
    633
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • P
      peili last edited by

      I do have the following classes:
      @#include <QApplication>
      #include <QComboBox>
      #include "mainwindow.h"
      MainWindow *w;
      int main(int argc, char *argv[])
      {
      QApplication a(argc, argv);
      MainWindow w;
      w.show();

      return a.exec&#40;&#41;;
      

      }
      @

      and

      @#include "mainwindow.h"

      MainWindow::MainWindow()
      {
      wtr = new WindowTitleReader();
      this->setWindowTitle("Selected Window Title Demo");
      }

      MainWindow::~MainWindow()
      {

      }@

      In WindowTitleReader i would like to add a QComboBox to MainWindow.
      I tried this Code:
      @ MainWindow * win = (MainWindow *) QApplication::activeWindow();
      QComboBox * combo = new QComboBox(win);@

      But it doesn't add my QComboBox to MainWindow.

      Any idea?

      1 Reply Last reply Reply Quote 0
      • SGaist
        SGaist Lifetime Qt Champion last edited by

        Hi,

        You are taking the wrong approach, please have a look at the examples and demos that comes with Qt. They'll show in details how to build a GUI

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply Reply Quote 0
        • First post
          Last post