Results 1 to 9 of 9

Thread: Absolute beginner in need of help

  1. #1
    Join Date
    Nov 2009
    Posts
    11
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Absolute beginner in need of help

    Hi

    I'm new to these forums and new to the world of Qt.

    Currently I'm working on a small assignment that asks me to create a window containing a label and a push button. When you click the button a new window should be generated, containing a line edit element (for the user to write something) and a push button. When you hit the button in this window, the window should close and the label in the first window should be updated with the text written in the line edit element (The second window is more or like a copy of QInputDialog). I would like to add that I do not use any designer for this project.

    I'm able to build the windows as required, but I am not sure how to generate/call the second window from the first one.

    I've named my classes MainWindow (containing a label and a push button) and EditWindow (containing line edit and push button). Both classes inherits from QDialog.

    In MainWindow I use the push button clicked as a signal and I've named the slot openNewWindow:
    Qt Code:
    1. connect(newWindowButton, SIGNAL(clicked()), this, SLOT(openNewWindow()));
    To copy to clipboard, switch view to plain text mode 

    The slot:
    Qt Code:
    1. void MainWindow::openNewWindow(){
    2. //QString txt = QInputDialog::getText(this, tr("Write text"), tr("Write some text:"));
    3.  
    4. //returnLabel->setText(txt);
    5.  
    6. // Some code to open a new window (EditWindow)
    7. }
    To copy to clipboard, switch view to plain text mode 

    As you can see I've tested the slot using QInputDialog and applying the text returned to returnLabel (the naming of the variables might be a bit bad).

    What I want to do in the slot is to open a the second window (EditWindow), but I'm not sure what the correct syntax for this would be to do this. I'm comfortable with one window, but not two or more.

    This is probably simple, but not being very confident in my own abilities in Qt at the moment, I would greatly appreciate some tips or suggestions.

    I hope this post is understandable, as English is not my first language.

    Regards,

    André

  2. #2
    Join Date
    Feb 2009
    Posts
    45
    Thanks
    3
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Windows

    Default Re: Absolute beginner in need of help

    Not a problem, that's what these forums are for.

    To call the second window you need to first instantiate the calling window and then execute it. It might look like this:

    Qt Code:
    1. void MainWindow::openNewWindow()
    2. {
    3. EditWindow myEditWindow(); // call the constructor of the second window
    4. myEditWindow.exec(); // This executes the window, and it will pop up
    5.  
    6. if (myEditWindow.result == QDialog::Accepted)
    7. {
    8. // Write your code here to retrieve what ever you want from the second window.
    9. // In the Second window you will need to create a slot for accept(), code below
    10. }
    11.  
    12. //You can then close or hide the second window
    13. myEditWindow.close();
    14.  
    15. }
    To copy to clipboard, switch view to plain text mode 

    In the Second window write the slot for accept() and link it to the 'clicked' signal (This depends on the buttons you have on your ui to close the second window Eg OK | Cancel or is it more buttons Eg: Save | Save All | Cancel).
    Using the accept() helps to link it to the appropriate button

    EditWindow.h file

    Qt Code:
    1. slot
    2. accept(); // For OK or Save
    3. reject(); // For cancel
    To copy to clipboard, switch view to plain text mode 

    EditWindow.cpp file
    Qt Code:
    1. connect (ui.OKButton, SIGNAL(clicked()), this, SLOT(accept()));
    2.  
    3. void EditWindow::accept()
    4. {
    5. QDialog::accept();
    6. //And whatever other code you might want to put here Eg validate before closing the window etc
    7.  
    8. }
    To copy to clipboard, switch view to plain text mode 

    Read the documents (Qt Assistant) for QDialog() and you will see the other slots there Eg open(), exec() etc

    Good luck!
    Last edited by qtUser500; 6th November 2009 at 16:38.

  3. #3
    Join Date
    Nov 2009
    Posts
    11
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Absolute beginner in need of help

    That was just what I've been looking for! Thank you very much!

    I will read the documentation for QDialog as I'm not completely sure about what's actually happening here.

    Thanks again

    Regards,

    André

  4. #4
    Join Date
    Feb 2009
    Posts
    45
    Thanks
    3
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Windows

    Default Re: Absolute beginner in need of help

    U r welcome
    A good place to start is reading the documentation.
    Search for 'An Overview of Qt's Examples' in QT Assistant and U will get an idea of how things work.

  5. #5
    Join Date
    Nov 2009
    Posts
    15
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Absolute beginner in need of help

    I'm doing something similar but I'm lost in some aspects... Could you put the code of the EditWindow Class. Thanks!

  6. #6
    Join Date
    Nov 2009
    Posts
    11
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Absolute beginner in need of help

    Here goes:

    Qt Code:
    1. EditWindow::EditWindow(QWidget* parent): QDialog(parent){
    2.  
    3. // Layout as you prefer it
    4.  
    5. connect(sendButton, SIGNAL(clicked()), this, SLOT(accept()));
    6.  
    7. }
    8.  
    9. void EditWindow::accept(){
    10. QDialog::accept();
    11. }
    12.  
    13. QString EditWindow::getText(){
    14. return textEdit->text();
    15. }
    To copy to clipboard, switch view to plain text mode 

  7. #7
    Join Date
    Nov 2009
    Posts
    15
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Absolute beginner in need of help

    I've tried to do this but I have an error with the code line "myEditWindow.exec();":
    Error 1 error C2228: the left operate of '.exec' must have class/struct/union.

    Any idea please?

  8. #8
    Join Date
    Nov 2009
    Posts
    11
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Absolute beginner in need of help

    I'm a newbie in Qt, so if anyone more qualified than me could answer, that would be nice.

    But the error message say that the left operate of myEditWindow.exec() must be of a class, etc. How did you declare myEditWindow?

    I did it this way from my MainWindow class:
    Qt Code:
    1. EditWindow ew(this);
    2. ew.exec();
    To copy to clipboard, switch view to plain text mode 

  9. #9
    Join Date
    Nov 2009
    Posts
    15
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Absolute beginner in need of help

    Thank you very much! The mistake was here. I forgot to put "this". I'm a newbie in Qt too, and I'm plenty of doubts and troubles...

    Now, I will try to put some buttons and other widgets in the second window. I hope that I don't disturb so much with my questions. Thanks a lot!

Similar Threads

  1. cannot find -lQtGui -QT Beginner Help Needed
    By nsa in forum Installation and Deployment
    Replies: 5
    Last Post: 18th July 2009, 07:31
  2. Absolute widget coordinates
    By rbp in forum Qt Programming
    Replies: 4
    Last Post: 28th January 2009, 04:30
  3. Beginner C++ question
    By masoroso in forum General Programming
    Replies: 2
    Last Post: 19th April 2006, 14:15
  4. QT4 beginner Fatal Error
    By Remyfr in forum Installation and Deployment
    Replies: 3
    Last Post: 11th March 2006, 01:48
  5. return of absolute coordinates in svg-graphics
    By hulk in forum Qt Programming
    Replies: 2
    Last Post: 22nd February 2006, 12:23

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.