Results 1 to 13 of 13

Thread: How to open another form in Qt4?

  1. #1
    Join Date
    Sep 2011
    Posts
    12
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default How to open another form in Qt4?

    Hello there! I am new to the forum, so just bear with me...

    I am trying to open another form by clicking a button on the first form. I tried...
    Qt Code:
    1. form1 w;
    2. w.show();
    To copy to clipboard, switch view to plain text mode 

    And it shows form2 then immediately closes form2.

    Qt Code:
    1. form1 w;
    2. w->show();
    To copy to clipboard, switch view to plain text mode 

    Gives compiler error: "form1.cpp:84: error: base operand of '->' has non-pointer type 'form2'"

    Qt Code:
    1. static Form2 *form2 = new Form2(this);
    2. form2->show();
    3. form2->activateWindow();
    4. form2->raise();
    To copy to clipboard, switch view to plain text mode 

    gives compiler error: "form1.h:21: error: ISO C++ forbids declaration of 'Form2' with no type."
    and error: "C:\Users\sapslaj\Desktop\chat-build-desktop\..\chat\mainlogin.h:21: error: expected ';' before '*' token"

    I have tried everything I know possible, and I cannot get it to stay open. Any help?

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

    Default Re: How to open another form in Qt4?

    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Sep 2011
    Posts
    12
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: How to open another form in Qt4?

    Its not QDialog derived. So still compiler errors.


    Added after 5 minutes:


    Here is the whole code.. Unmodified in any way.
    Qt Code:
    1. #include "mainlogin.h"
    2. #include "ui_mainlogin.h"
    3. #include <QFile>
    4. #include <QMessageBox>
    5. #include "mainwin.h"
    6. #include "ui_mainwin.h"
    7. #include <QCloseEvent>
    8. #include <QTextStream>
    9.  
    10.  
    11. mainlogin::mainlogin(QWidget *parent) :
    12. QWidget(parent),
    13. ui(new Ui::mainlogin)
    14. {
    15. ui->setupUi(this);
    16. }
    17.  
    18. mainlogin::~mainlogin()
    19. {
    20. // do nothing
    21. }
    22.  
    23. void mainlogin::on_bLogin_clicked()
    24. {
    25. mainlogin::login();
    26. }
    27.  
    28. void mainlogin::login()
    29. {
    30. QFile loginsFile("/logins.txt");
    31. /*
    32.   if(loginsFile.size() == 0 || loginsFile.exists() == false)
    33.   {
    34.   QMessageBox error;
    35.   error.setText("There are no logins stored. Please reinstall the program.");
    36.   error.exec();
    37.   return;
    38.   }*/
    39. QTextStream loginsStream(&loginsFile);
    40.  
    41. QString enteredUsername = ui->leUsername->text();
    42. QString enteredPassword = ui->lePassword->text();
    43.  
    44. QString loadedLogins = loginsStream.readAll();
    45.  
    46. int usernameCorrect = 0;
    47.  
    48. QString filedUsername = loadedLogins.section("|", 1, 1);
    49. QString filedPassword = loadedLogins.section("|", 2, 2);
    50.  
    51. // for debug puposes only, until i can figure out whats causing all of the crap with the login.
    52. filedUsername = "sapslaj";
    53. filedPassword = "gamilo90";
    54.  
    55. //printf("enteredUsername: %s\nenteredPassword: %s\nloadedLogins: %s\nfiledUsername: %s\nfiledPassword: %s", enteredUsername.toStdString(), enteredPassword.toStdString(), loadedLogins.toStdString(), filedUsername.toStdString(), filedPassword.toStdString());
    56.  
    57. /*QMessageBox debug;
    58.   debug.setText(enteredUsername);
    59.   debug.exec();
    60.   debug.setText(enteredPassword);
    61.   debug.exec();
    62.   debug.setText(loadedLogins);
    63.   debug.exec();
    64.   debug.setText(filedUsername);
    65.   debug.exec();
    66.   debug.setText(filedPassword);
    67.   debug.exec();*/
    68.  
    69.  
    70.  
    71. if(enteredUsername != filedUsername)
    72. {
    73. QMessageBox error;
    74. error.setText("Username not found!");
    75. error.exec();
    76. usernameCorrect = 0;
    77. }
    78.  
    79. else
    80. {
    81. if(enteredPassword == filedPassword)
    82. {
    83. mainwin form(this);
    84. form.exec();
    85.  
    86.  
    87.  
    88. //form2->show();
    89. //form2->activateWindow();
    90. //form2->raise();
    91.  
    92.  
    93. //mainlogin::close();
    94. }
    95.  
    96. else
    97. {
    98. QMessageBox error;
    99. error.setText("Login Failed");
    100. error.exec();
    101. }
    102. }
    103. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by sapslaj; 2nd September 2011 at 01:50.

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

    Default Re: How to open another form in Qt4?

    Read the link again. There is no assumption about any dialog there.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: How to open another form in Qt4?

    Quote Originally Posted by sapslaj View Post
    I am trying to open another form by clicking a button on the first form. I tried...
    Qt Code:
    1. form1 w;
    2. w.show();
    To copy to clipboard, switch view to plain text mode 
    And it shows form2 then immediately closes form2.
    What is the lifetime of the stack-allocated w object in C++? This is basic C++, not a Qt question.
    Given that w.show() is non-blocking, why do you think the window might go away?

    Qt Code:
    1. form1 w;
    2. w->show();
    To copy to clipboard, switch view to plain text mode 

    Gives compiler error: "form1.cpp:84: error: base operand of '->' has non-pointer type 'form2'"
    What do you think the error message is telling you? This is basic C++, not a Qt question. You might find some clues in your next snippet.

    Qt Code:
    1. static Form2 *form2 = new Form2(this);
    2. form2->show();
    3. form2->activateWindow();
    4. form2->raise();
    To copy to clipboard, switch view to plain text mode 

    gives compiler error: "form1.h:21: error: ISO C++ forbids declaration of 'Form2' with no type."
    and error: "C:\Users\sapslaj\Desktop\chat-build-desktop\..\chat\mainlogin.h:21: error: expected ';' before '*' token"
    Where is the class Form2 declared? This is basic C++, not a Qt question.

    You may have noticed a theme in my questions. You have all the elements of the Qt side of this problem here and in the FAQ page wysota gave you but your C++ knowledge is letting you down.

  6. The following user says thank you to ChrisW67 for this useful post:

    megazig (2nd September 2011)

  7. #6
    Join Date
    Sep 2011
    Posts
    12
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: How to open another form in Qt4?

    Well I posted in the Newbie forum for a reason, then... I'm not an expert at C++ at all. Now I will post the whole project if you want me to, so someone can help me out... I'm a full n00b at Qt. If someone can come up with a solution, so I can learn.


    Added after 24 minutes:


    Assuming you have two form classes: Form1 and Form2 and that Form1 has a pushbutton "button" child and Form2 is derived from QDialog.
    It is not derived from QDialog.
    Last edited by sapslaj; 2nd September 2011 at 18:59.

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

    Default Re: How to open another form in Qt4?

    Quote Originally Posted by sapslaj View Post
    Well I posted in the Newbie forum for a reason, then...
    The Newbie forum is for newbies in Qt, not newbies in programming. If you have problems with C++, post in "General Programming".

    It is not derived from QDialog.
    Did you even try following the advice?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  9. #8
    Join Date
    Sep 2011
    Posts
    12
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: How to open another form in Qt4?

    I'm not a noob to programming, I'm a noob to Qt. I have followed the advice line for line. It works with QDialog. Not QWidget for whatever reason. I'm not stating this as a problem with Qt. In fact, I love working with Qt. Best thing that has ever happened to me. But I'm a total noob at it. And I have no idea what I am doing wrong. We are all human, and we make mistakes. Now if you see a problem, I wish for you to just speak out. Don't try to make me figure it out on my own. I will probably never come back to this forum ever again, because I have had no problems with Qt up until this point. Although I have a love for Qt, and would be more than happy to help anybody on here if they have problems, you apparently have very little care for my problem. So forget I ever asked. You people apparently refuse to help me solve the problem. Although I appreciate what little help you did give me... I give up, and moving to another forum.

  10. #9
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: How to open another form in Qt4?

    You have the entire problem solution in this thread: what more do you expect? The problems you are having are not Qt problems, they are C++ programming problems. Looking back a post #5....

    • Your first attempt fails because the object that creates and displays the widget goes out of scope.
    • Your second attempt fails because you try to treat an instance of an object as if it were a pointer to an object. These are not the same thing, and it would not matter if the object were not of Qt origin.
    • Your third attempt, which is almost exactly the solution in the FAQ you have been referred to, fails because you have tried to use a class that is not defined.

    This forum operates on the basis that it is better to learn something than than simply copy and paste answers. So, here is a complete, didactic example:
    Qt Code:
    1. #include <QtGui>
    2.  
    3. class SecondWidget: public QWidget {
    4. Q_OBJECT
    5. public:
    6. SecondWidget(QWidget *p = 0): QWidget(p) {
    7. setAttribute(Qt::WA_DeleteOnClose); // make sure memory is cleaned up
    8. resize(320, 240);
    9.  
    10. QVBoxLayout *layout = new QVBoxLayout;
    11. layout->addWidget( new QLabel("Label 1", this) );
    12. layout->addWidget( new QLabel("Label 2", this) );
    13. setLayout(layout);
    14. }
    15. };
    16.  
    17. class MainWindow: public QMainWindow {
    18. Q_OBJECT
    19. public:
    20. MainWindow(QWidget *p = 0): QMainWindow(p) {
    21. QPushButton *pb = new QPushButton("Push me!", this);
    22. connect(pb, SIGNAL(clicked()), SLOT(onClick()));
    23. setCentralWidget(pb);
    24. }
    25. public slots:
    26. void onClick() {
    27. SecondWidget *a = new SecondWidget;
    28. a->show();
    29. }
    30. private:
    31. };
    32.  
    33. int main(int argc, char *argv[])
    34. {
    35. QApplication app(argc, argv);
    36.  
    37. MainWindow m;
    38. m.show();
    39. return app.exec();
    40. }
    41. #include "main.moc"
    To copy to clipboard, switch view to plain text mode 
    Please try to learn from it. In particular, there is no reference to QDialog, the onClick() handler looks strikingly similar to your third attempt, the second widget opens on demand and stay open even if your close the first widget. I leave it as an exercise to limit the program to a single second widget.
    Last edited by ChrisW67; 3rd September 2011 at 04:36.

  11. #10
    Join Date
    Sep 2011
    Posts
    12
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: How to open another form in Qt4?

    Your solution helped. I didn't exactly need direct code, I just needed a simple solution instead of someone asking me questions to figure this out. I taught myself C++ using absolutly no tutorials. I just examined other programs and figured out how they work. I couldn't find another program that had the solution I was trying to fix, but just that simple program that you posted is all I need. I can learn from that. Thanks.

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

    Default Re: How to open another form in Qt4?

    Quote Originally Posted by sapslaj View Post
    Your solution helped. I didn't exactly need direct code, I just needed a simple solution instead of someone asking me questions to figure this out. I taught myself C++ using absolutly no tutorials. I just examined other programs and figured out how they work. I couldn't find another program that had the solution I was trying to fix, but just that simple program that you posted is all I need. I can learn from that. Thanks.
    Did you notice the code Chris gave you is identical to the one in the FAQ entry I posted?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  13. #12
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: How to open another form in Qt4?

    The example in the FAQ is subtly different although the intent is the same.

    As written, the FAQ entry does require the second form to be a QDialog in order to function as expected. If you simply substitute a QWidget for QDialog in the FAQ example the widget is drawn as a child of the first form, not an independent window. This happens because the widget is parented. QDialog forces an independent window regardless of the dialog being parented. For example, with my code, line 7 commented and line 27 changed to:
    Qt Code:
    1. static SecondWidget *a = new SecondWidget(this);
    To copy to clipboard, switch view to plain text mode 
    in line with the FAQ. This is the result after pushing the button (I enlarged the button to see more clearly):
    screen2.png
    This is why I do not parent the widget when I create it. I assume this is what the OP was seeing (or not seeing) when trying the FAQ as-is.

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

    Default Re: How to open another form in Qt4?

    I updated the FAQ entry a bit.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Replies: 3
    Last Post: 11th June 2012, 15:21
  2. Replies: 7
    Last Post: 8th October 2010, 13:07
  3. Open one Form (widget)
    By vinny gracindo in forum Newbie
    Replies: 1
    Last Post: 7th October 2009, 13:13
  4. Replies: 10
    Last Post: 6th July 2008, 09:46
  5. Replies: 3
    Last Post: 13th May 2007, 20:55

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
  •  
Qt is a trademark of The Qt Company.