[solved]QDialog interacts on QMainWindow



  • 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 :)



  • 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.



  • 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().



  • 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.



  • 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 :)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.