Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [solved]QDialog interacts on QMainWindow
Forum Updated to NodeBB v4.3 + New Features

[solved]QDialog interacts on QMainWindow

Scheduled Pinned Locked Moved General and Desktop
5 Posts 4 Posters 5.2k Views 1 Watching
  • 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.
  • B Offline
    B Offline
    BlackDante
    wrote on last edited by
    #1

    Hi All, I am writing to you with another problem :)
    In my aplication I have a popup window who was created from QDialog class and I have a mainwindow from QMainWindow.. So how I can get to object QMainWindow in file mainwindow.cpp but Object QMainWindow is creating in file main.cpp it is something like that
    @int main(int argc,char *argv[]){
    QApplication a(argc, argv);
    MainWindow w;
    w.show();
    return a.exec();
    }@
    @
    MainWindow::MainWindow(QWidget parent) : QMainWindow(parent){
    //some code
    QDialog
    dialog = new MyDialog(this);
    dialog->createdialog();
    }
    void MyDialog::createdialog(){
    //some code
    }
    @
    and my question is how I can get to this object(MainWindow w) in class MyDialog?
    I hope that I explain my problem well :) and thanks for any help :)

    sorry for my broken english :)

    1 Reply Last reply
    0
    • R Offline
      R Offline
      rich
      wrote on last edited by
      #2

      This is really basic, you should look at some books about how to program in c++. You can solve this by adding another parameter to your 'MyDialog' constructor that takes a pointer to your main window class.

      1 Reply Last reply
      0
      • G Offline
        G Offline
        giesbert
        wrote on last edited by
        #3

        Why do you need another parameter?
        @
        MainWindow::MainWindow(QWidget parent) : QMainWindow(parent){
        //some code
        QDialog
        dialog = new MyDialog(this);
        dialog->createdialog();
        }
        @

        The constructor of MyDialog gets a pointer to the object. So you already have it, it's stored in parent().

        Nokia Certified Qt Specialist.
        Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

        1 Reply Last reply
        0
        • ZlatomirZ Offline
          ZlatomirZ Offline
          Zlatomir
          wrote on last edited by
          #4

          I'm answering only for second question, since the first one seems you already are on the correct path ;) (i just have one small naming suggestion about createdialog(), use a different name, since it doesn't create the dialog... call it initialize_WHAT_IT_INITIALIZE, or some name that has some meaning)
          Also in the code you give you need the object name to call the member function:
          @QDialog* dialog = new MyDialog(this);
          dialog->createdialog(); @

          [quote author="BlackDante" date="1295297253"]and my question is how I can get to this object(MainWindow w) in class MyDialog?[/quote]

          The correct (OOP) way is to code getter and/or setter methods for all the components of MainWindow that you need to access from outside.
          Setters are good candidates for being declared as slots.

          Also if you used Designer and the ui pointer is private (as it should be, since is better encapsulated that way) you will need some more get/set (may be slots) for the ui components.

          You can somehow hack this out by passing pointers around and use them to do your work, but the correct way is encapsulate - code get and/or set functions - use signals and slots to connect the functionality of your application.

          https://forum.qt.io/category/41/romanian

          1 Reply Last reply
          0
          • B Offline
            B Offline
            BlackDante
            wrote on last edited by
            #5

            oh, I am so stupid, I lost my last 4 hours and this was that simple :/ so I am much grateful for yours anwsers :) Thank you very much :)

            sorry for my broken english :)

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved