Results 1 to 13 of 13

Thread: QDialog and QMainWindow Data Transfer

  1. #1
    Join Date
    Mar 2007
    Posts
    16
    Thanks
    9
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QDialog and QMainWindow Data Transfer

    Can anyone guide me,
    i have created my MainWindow and in its constructor i call a Dialog window named LoginWindow which inherits QDialog, this Dialog is modal and made of two lineEdits and three Buttons my problem is that i want when user clicks the "validerButton", i can transfer the texts typed by the user in the LineEdits to my MainWindow from where i called this Dialog.


    this is my MainWindow s constructor
    Qt Code:
    1. MainWindow::MainWindow()
    2. {
    3. createActions();
    4. createMenus();
    5. createToolBars();
    6. createStatusBar();
    7.  
    8. readSettings();
    9.  
    10. //this is where my problem starts
    11. login = new Loginwindow(this);
    12.  
    13. login->show();
    14. //i tried this but i get a segmentation error
    15. connect (login->validerButton, SIGNAL(clicked()), this, SLOT(getNames()));
    16. }
    17.  
    18. void MainWindow::getNames()
    19. {
    20. QString text;
    21. text = login->getNumetudLineEdit();
    22. }
    To copy to clipboard, switch view to plain text mode 

    i send u also my LoginWindow

    Qt Code:
    1. Loginwindow::Loginwindow(QWidget * parent) : QDialog (parent)
    2. {
    3. //construction des labels et des lignes edit
    4.  
    5. NumetudLabel = new QLabel(tr("&Numero Etudiant:"));
    6. NumetudLineEdit = new QLineEdit;
    7. NumetudLabel->setBuddy(NumetudLineEdit);
    8.  
    9. CodesecretLabel = new QLabel(tr("&Code Secret:"));
    10. CodesecretLineEdit = new QLineEdit;
    11. CodesecretLabel->setBuddy(CodesecretLineEdit);
    12.  
    13. QPushButton * inscriptionButton;
    14. QPushButton * inviteButton;
    15. QPushButton * validerButton;
    16.  
    17. inscriptionButton = new QPushButton(tr("&S'inscrire"));
    18. inscriptionButton->setDefault(true);
    19. inviteButton = new QPushButton(tr("&Invité"));
    20. validerButton = new QPushButton(tr("&Valider"));
    21.  
    22. //disposition des layouts
    23. topLeftLayout = new QVBoxLayout;
    24. topLeftLayout->addWidget(NumetudLabel);
    25. topLeftLayout->addWidget(NumetudLineEdit);
    26. topLeftLayout->addWidget(CodesecretLabel);
    27. topLeftLayout->addWidget(CodesecretLineEdit);
    28.  
    29. rightLayout = new QVBoxLayout;
    30. rightLayout->addWidget(validerButton);
    31. rightLayout->addWidget(inscriptionButton);
    32. rightLayout->addWidget(inviteButton);
    33. rightLayout->addStretch();
    34.  
    35. //QVBoxLayout *leftLayout = new QVBoxLayout;
    36. //leftLayout->addLayout(topLeftLayout);
    37.  
    38. mainLayout = new QHBoxLayout;
    39. mainLayout->addLayout(topLeftLayout);
    40. mainLayout->addLayout(rightLayout);
    41. this->setLayout(mainLayout);
    42.  
    43. this->setWindowTitle(tr("Login"));
    44. this->setModal(true);
    45. this->setSizeGripEnabled(false);
    46. //setFixedHeight(sizeHint().height());
    47.  
    48. //les signaux et slots
    49. connect(validerButton, SIGNAL(clicked()), this, SLOT(Valider()));
    50. }
    To copy to clipboard, switch view to plain text mode 

    Pls help me, it would be nice of u
    Last edited by wysota; 5th May 2007 at 08:38. Reason: missing [code] tags

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QDialog and QMainWindow Data Transfer

    But what is the problem? If you have a pointer to the dialog then just access appropriate members of the dialog (either directly if they are public or through some getter method you need to implement).

  3. #3
    Join Date
    Apr 2007
    Posts
    44
    Thanks
    6
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: QDialog and QMainWindow Data Transfer

    //i tried this but i get a segmentation error connect (login->validerButton, SIGNAL(clicked()), this, SLOT(getNames()));

    you have to connect signal before

    login->show();

  4. The following user says thank you to Teerayoot for this useful post:

    Ahmad (5th May 2007)

  5. #4
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QDialog and QMainWindow Data Transfer

    Quote Originally Posted by Teerayoot View Post
    //i tried this but i get a segmentation error connect (login->validerButton, SIGNAL(clicked()), this, SLOT(getNames()));

    you have to connect signal before

    login->show();
    Why is that? Signals can be connected and disconnected dynamically - therefore it shouldn't matter where/when you connect a signal.

    Regards

  6. #5
    Join Date
    Apr 2007
    Posts
    44
    Thanks
    6
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: QDialog and QMainWindow Data Transfer

    ohh,i never know before.
    i alway connect signal after object create.
    Thank you.

  7. #6
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QDialog and QMainWindow Data Transfer

    Sounds like either login or validerButton is an uninitialized pointer.
    J-P Nurmi

  8. #7
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QDialog and QMainWindow Data Transfer

    Quote Originally Posted by jpn View Post
    Sounds like either login or validerButton is an uninitialized pointer.
    No, they're OK both ( look at the code in the first post ).
    I am more concerned with the fact that some of the widgets in the loginwindow do not have parents, and that the dialog is modal.

    Too laisy now to make my own example and test it ( it's saturday )

    regards

  9. The following user says thank you to marcel for this useful post:

    Ahmad (5th May 2007)

  10. #8
    Join Date
    Mar 2007
    Posts
    16
    Thanks
    9
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QDialog and QMainWindow Data Transfer

    TNX EVERYONE FOR UR REPLIES
    but i still have problems, the problem is the same, i dont want to get the text that the user types directly when he clicks validerButton, it would have been easy, but i want to pass by a a validation methode, i should test if this is the text that i want... and if it is ok i should pass that text to my MainWindow, otherwise i should always stay in my LoginWindow which is a Modal Dialog, i send u an example of what i want with this... but as always i get a segmentation error
    tnx for ur helps and suggestions,

    lots of code parts are not concerned with this problem but i send u all of the code and the variable names are in french, so hope u dont get bored with them.. lol
    Attached Files Attached Files

  11. #9
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QDialog and QMainWindow Data Transfer

    Hey, I kind of found your error:

    It was in the contstructor of your loginwindow, because you had:

    Qt Code:
    1. QPushButton * inscriptionButton;
    2. QPushButton * inviteButton;
    3. QPushButton * validerButton;
    4.  
    5. inscriptionButton = new QPushButton(tr("&S'inscrire"), this);
    6. inscriptionButton->setDefault(true);
    7. //inscriptionButton->setEnabled(true); Pas besoin de le mettre, par defaut
    8.  
    9. //le & plac�devant annuler sert a creer un raccourci avec alt + a
    10. inviteButton = new QPushButton(tr("&Invité"), this);
    To copy to clipboard, switch view to plain text mode 
    Look at the first three lines. You redeclared the push buttons here. They are also members. If you remove those three lines, it is OK.

    Here's the main wnd constructor, which I used when testing.
    Qt Code:
    1. MainWindow::MainWindow()
    2. {
    3. createActions();
    4. createMenus();
    5. createToolBars();
    6. createStatusBar();
    7.  
    8. readSettings();
    9.  
    10. login = new Loginwindow(this);
    11. login->show();
    12. QString text;
    13. QString text2;
    14.  
    15. connect( login->validerButton, SIGNAL( clicked() ), this, SLOT( getNames() ) );
    16. }
    To copy to clipboard, switch view to plain text mode 

    Anyway, your solution is not so good.
    Why don't you emit a signal for LoginWindow::Valider, which will be connected to the main window, in the slot getNames. When you press "validate" you don't know if the data is valid ( I assume that Valider() decides if the entered data is correct ), therefore Valider() should emit a signal to indicate the data is correct.

    Regards

  12. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QDialog and QMainWindow Data Transfer

    How about using a QValidator object to validate the input directly in the text field? You can then emit a signal from your validate() method and connect it to a button that accepts the dialog (like "Ok", "Login" or whatever you have there) to automatically enable/disable the button to make sure the user can't leave the dialog without entering proper data. Also remember to reimplement the close event to prevent the user from closing the window without entering proper data (unless you want him to be able to cancel the dialog).

  13. #11
    Join Date
    May 2007
    Posts
    16
    Thanked 1 Time in 1 Post

    Default Re: QDialog and QMainWindow Data Transfer

    1) In Loginwindow::Valider() emit the signal with an argument QVariant, in your case CodesecretLineEdit.text() becomes QVariant

    Qt Code:
    1. Loginwindow::Valider()
    2. {
    3. emit validerButtonClicked(CodesecretLineEdit.text());
    4. }
    To copy to clipboard, switch view to plain text mode 

    2) Listen to the above signal validerButtonClicked in MainWindow class
    Qt Code:
    1. MainWindow::MainWindow()
    2. {
    3. connect (login, SIGNAL(validerButtonClicked(const QVariant &)), this, SLOT(ValiderAction(QVariant &)));
    4. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. MainWindow::ValiderAction(QVariant value)
    2. {
    3. // now data has come to your mainwindow
    4. // do whatever u want.
    5. }
    To copy to clipboard, switch view to plain text mode 

    Thanks
    c v rao.
    Last edited by wysota; 5th May 2007 at 18:27. Reason: missing [code] tags and incorrect class names

  14. The following user says thank you to chikkireddi for this useful post:

    Ahmad (5th May 2007)

  15. #12
    Join Date
    Mar 2007
    Posts
    16
    Thanks
    9
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QDialog and QMainWindow Data Transfer

    Tnx everyone,
    i find the solution for my problem, in fact the post by Teerayoot was not bad, i putted everytime my signal connection in mainWindow after the login-> exec() function and that was the problem, once i putted just after the creation of login and before its execution, it worked , woow i m happy

  16. #13
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QDialog and QMainWindow Data Transfer

    i find the solution for my problem, in fact the post by Teerayoot was not bad, i putted everytime my signal connection in mainWindow after the login-> exec() function and that was the problem, once i putted just after the creation of login and before its execution, it worked , woow i m happy
    I do not agree with this.

    As jpn said in a previous post, you got a segfault because of an invalid pointer. But this pointer was not invalid because you didn't initialized it, but because you had the same variable declared as member and as local, in the constructor of the login window. This caused the local var to be initialiased, the member remaining invalid.

    This can be seen very well in the code you posted.

    So, you either modified this too ( once you did this, you could have moved the connect anywhere - given that the objects were created - it still would have worked ), or you have completely rewritten parts of your code.

    But by simply moving the connect to another line you will never make it work.

    Regards

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.