Results 1 to 7 of 7

Thread: Qt Creator - Main widow and second window

  1. #1
    Join Date
    Dec 2018
    Posts
    9
    Thanks
    7
    Qt products
    Qt5
    Platforms
    Windows

    Default Qt Creator - Main widow and second window

    how do you hide the main Window after opening the second Window?Also, when the second Window is closed by the user, how can the main window reappear?

    these are the codes I have so far.
    I have an error that says ERROR:no matching constructor for initialization 'Dialog'...it's at the mainwindow.cpp ...this is the only error I have. Please help me fix it.

    .H FILES

    mainwidow.h
    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include <QMainWindow>
    5.  
    6. namespace Ui {
    7. class MainWindow;
    8. }
    9.  
    10. class MainWindow : public QMainWindow
    11. {
    12. Q_OBJECT
    13.  
    14. public:
    15. explicit MainWindow(QWidget *parent = nullptr);
    16. ~MainWindow();
    17.  
    18. public:
    19. void show();
    20.  
    21.  
    22.  
    23. private slots:
    24. void on_pushButton_clicked();
    25.  
    26. private:
    27. Ui::MainWindow *ui;
    28. };
    29.  
    30. #endif // MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 



    secwindow.h

    Qt Code:
    1. #ifndef SECWINDOW_H
    2. #define SECWINDOW_H
    3.  
    4. #include <QDialog>
    5.  
    6. namespace Ui {
    7. class SecWindow;
    8. }
    9.  
    10. class SecWindow : public QDialog
    11. {
    12. Q_OBJECT
    13.  
    14. public:
    15. explicit SecWindow(QWidget *parent = nullptr);
    16. ~SecWindow();
    17.  
    18.  
    19. private:
    20. Ui::SecWindow *ui;
    21. };
    22.  
    23. class Dialog : public QDialog
    24. {
    25. public:
    26. Dialog();
    27.  
    28. private:
    29. Dialog *dialog;
    30.  
    31. };
    32.  
    33.  
    34. #endif // SECWINDOW_H
    To copy to clipboard, switch view to plain text mode 


    SOURCE CODE

    main.cpp

    Qt Code:
    1. #include "mainwindow.h"
    2. #include <QApplication>
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QApplication a(argc, argv);
    7. a.setQuitOnLastWindowClosed(false);
    8. MainWindow w;
    9. w.show();
    10.  
    11. return a.exec();
    12. }
    To copy to clipboard, switch view to plain text mode 

    mainwindow.cpp

    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3. #include <QMessageBox>
    4. #include <QPixmap>
    5. #include "secwindow.h"
    6. #include <QDialog>
    7.  
    8. MainWindow::MainWindow(QWidget *parent) :
    9. QMainWindow(parent),
    10. ui(new Ui::MainWindow)
    11. {
    12. ui->setupUi(this);
    13. QPixmap pix("C:/Users/Charlene/Downloads/Charlene Back-up/MAPUA/2nd Term/Object Oriented Programming/GOW-Gui/GOW-GUI/intro pic/intro.png");
    14. ui->label->setPixmap(pix.scaled(230,250,Qt::KeepAspectRatio));
    15.  
    16. // QWidget *wdg = new QWidget;
    17. //wdg->show();
    18. //hide();
    19. }
    20.  
    21. MainWindow::~MainWindow()
    22. {
    23. delete ui;
    24. }
    25.  
    26. void MainWindow::on_pushButton_clicked() // Modal approach..mainwindow cannot be moved when secwindow is displayed.
    27. {
    28. SecWindow secwindow;
    29. secwindow.setModal(true); //it'll set the secwindow
    30. secwindow.exec(); //shows secwindow when button is pressed
    31. }
    32.  
    33. void MainWindow::show()
    34. {
    35. Dialog *dialog = new Dialog(this); //ERROR: no matching constructor for initialization of 'Dialog'
    36. connect(dialog, SIGNAL(accepted()), this, SLOT(show()));
    37. connect(dialog, SIGNAL(rejected()), this, SLOT(show()));
    38. dialog->show();
    39. hide();
    40. };
    To copy to clipboard, switch view to plain text mode 



    secwindow.cpp

    Qt Code:
    1. #include "secwindow.h"
    2. #include "ui_secwindow.h"
    3. #include <QPixmap>
    4.  
    5. SecWindow::SecWindow(QWidget *parent) :
    6. QDialog(parent),
    7. ui(new Ui::SecWindow)
    8. {
    9. ui->setupUi(this);
    10. QPixmap pix("C:/Users/Charlene/Downloads/Charlene Back-up/MAPUA/2nd Term/Object Oriented Programming/GOW-Gui/GOW-GUI/images/102.gif");
    11. ui->label_2 ->setPixmap(pix.scaled(100,95, Qt::KeepAspectRatio));
    12. }
    13.  
    14. SecWindow::~SecWindow()
    15. {
    16. delete ui;
    17. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    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: Qt Creator - Main widow and second window

    Quote Originally Posted by Kuushie118 View Post
    how do you hide the main Window after opening the second Window?Also, when the second Window is closed by the user, how can the main window reappear?

    these are the codes I have so far.
    I have an error that says ERROR:no matching constructor for initialization 'Dialog'...it's at the mainwindow.cpp ...this is the only error I have. Please help me fix it.
    The error message is because there is no class named "Dialog". Did you perhaps mean QDialog?

    Without a modal window (i.e. no call to exec()) this is one way to go about it:
    Qt Code:
    1. #include <QApplication>
    2. #include <QMainWindow>
    3. #include <QPushButton>
    4. #include <QWidget>
    5. #include <QLabel>
    6. #include <QVBoxLayout>
    7. #include <QCloseEvent>
    8.  
    9. class SecondWindow: public QWidget {
    10. Q_OBJECT
    11. public:
    12. explicit SecondWindow(QWidget *parent = nullptr);
    13. ~SecondWindow();
    14. protected:
    15. void closeEvent(QCloseEvent *event);
    16. signals:
    17. void closing();
    18. };
    19.  
    20. SecondWindow::SecondWindow(QWidget *parent):
    21. QWidget(parent)
    22. {
    23. // Dummy UI
    24. QVBoxLayout *layout = new QVBoxLayout(this);
    25. QLabel *l = new QLabel("The Second Widget", this);
    26. layout->addWidget(l);
    27. }
    28.  
    29. SecondWindow::~SecondWindow() { }
    30.  
    31. void SecondWindow::closeEvent(QCloseEvent *event)
    32. {
    33. emit closing();
    34. event->accept();
    35. }
    36.  
    37.  
    38. class MainWindow: public QMainWindow {
    39. Q_OBJECT
    40. public:
    41. explicit MainWindow(QWidget *parent = nullptr);
    42. ~MainWindow();
    43.  
    44. private slots:
    45. void on_pushButton_clicked();
    46. private:
    47. SecondWindow *m_secondWindow;
    48. };
    49.  
    50. MainWindow::MainWindow(QWidget *parent):
    51. QMainWindow(parent)
    52. {
    53. // Simple UI
    54. QPushButton *pb = new QPushButton("Push Me", this);
    55. connect(pb, SIGNAL(clicked()), this, SLOT(on_pushButton_clicked()));
    56. setCentralWidget(pb);
    57.  
    58. // Second window ready to go
    59. m_secondWindow = new SecondWindow();
    60. connect(m_secondWindow, SIGNAL(closing()), this, SLOT(show()));
    61. }
    62.  
    63. MainWindow::~MainWindow()
    64. {
    65. delete m_secondWindow;
    66. }
    67.  
    68. void MainWindow::on_pushButton_clicked() {
    69. hide();
    70. m_secondWindow->show();
    71. }
    72.  
    73. int main(int argc, char **argv) {
    74. QApplication app(argc, argv);
    75. MainWindow w;
    76. w.show();
    77. return app.exec();
    78. }
    79.  
    80. #include "main.moc"
    To copy to clipboard, switch view to plain text mode 

    Essentially you make the second window emit a signal as it closes and use that to unhide the main window.
    Last edited by ChrisW67; 28th December 2018 at 06:52.
    "We can't solve problems by using the same kind of thinking we used when we created them." -- Einstein
    If you are posting code then please use [code] [/code] tags around it - makes addressing the problem easier.

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

    Kuushie118 (30th December 2018)

  4. #3
    Join Date
    Dec 2018
    Posts
    9
    Thanks
    7
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Qt Creator - Main widow and second window

    but sir, is there a way to fix my error?
    My professor wants me to do it that way

    you say that my error is that I don't have a class dialog.
    but don't I have one at secwindow.h line 23??


    Added after 59 minutes:


    I edited the void MainWindow::show();

    Qt Code:
    1. void MainWindow::show()
    2. {
    3. QDialog *dialog = new QDialog(this);
    4. connect(dialog, SIGNAL(accepted()), this, SLOT(show()));
    5. connect(dialog, SIGNAL(rejected()), this, SLOT(show()));
    6. dialog->show();
    7. hide();
    8. };
    To copy to clipboard, switch view to plain text mode 

    there's no error anymore but it just doesn't show my main window.

    it says that:
    QObject::connect: (sender name: 'MainWindow')
    QObject::connect: (receiver name: 'MainWindow')


    It just shows like a blank window first, then when I close that blank window...my main window will appear(in this part, the modal approach still works)
    how can I fix this please?
    Last edited by Kuushie118; 28th December 2018 at 09:47.

  5. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Qt Creator - Main widow and second window

    void MainWindow::show()
    {
    Dialog *dialog = new Dialog(this); //ERROR: no matching constructor for initialization of 'Dialog'
    connect(dialog, SIGNAL(accepted()), this, SLOT(show()));
    connect(dialog, SIGNAL(rejected()), this, SLOT(show()));
    dialog->show();
    hide();
    };
    As ChrisW67 said, your error is because you don't have any class named "Dialog". If you change Dialog to QDialog, then this code will compile, but it will create a completely empty, blank dialog (since a QDialog instance has no content, it's just an empty widget with a frame).

    What you probably mean to do in this code is to create an instance of your "SecWindow" class and show that.

    But there is a bigger problem here - you have chosen to name this method "show()", which overrides the QMainWindow::show() slot. So you are basically causing this method to execute itself over and over again. Here's what is probably actually happening in your code:

    1 - You (or the Qt framework) call QMainWindow::show().
    2 - Because you have overridden this method by implementing MainWindow::show(), your method gets called instead.
    3 - Your method creates a QDialog and connects its signals to this MainWindow::show() method.
    4 - The dialog then opens and the MainWindow is hidden.
    5 - When you close the dialog, it executes the MainWindow::show() method again (because of the connection to the dialog's signals)
    6 - This creates a -new- QDialog (different from the first one), and you go back to Step 3 of this list.

    Each time you go through this loop, your code creates a new QDialog. Even though you close it by clicking OK or Cancel, it doesn't actually go away, it just becomes a ghost with a "dangling pointer". It isn't deleted, it just stays there invisible. The next time show() executes, it creates -another- QDialog instance, different from the first one, and it also ends up as an invisible ghost. And on and on.

    (If you have not defined MainWindow::show() as a slot this might not happen, because the connect() statement only allows connection to slots if you use the version of connect() with the SIGNAL() and SLOT() macros. This could explain why Qt complains about your connect() calls. And if the connect() is failing, then closing the dialog won't make the MainWindow reappear).

    The solution involves two fixes:

    1 - Rename your MainWindow::show() to something else that isn't the name of a QMainWindow or other base class method, like showDialog() maybe. Then your connections to QMainWindow::show() will work correctly and Qt won't complain.
    2 - Make your dialog a member variable of the MainWindow class and rewrite showDialog() so it simply uses the same SecWindow instance over and over:

    Qt Code:
    1. // MainWindow.h:
    2.  
    3. class MainWindow : public QMainWindow
    4. {
    5. Q_OBJECT;
    6.  
    7. MainWindow( QWidget * parent );
    8.  
    9. // ...
    10.  
    11. private slots:
    12. void showDialog();
    13.  
    14. private:
    15. SecWindow * secWindow = nullptr;
    16. };
    17.  
    18. // Mainindow.cpp
    19.  
    20. MainWindow::MainWindow( QWidget * parent ) : QMainWindow( parent )
    21. {
    22. secWindow = new SecWindow( this );
    23. connect( secWindow, &SecWindow::accepted, this, &QMainWindow::show ); // connect to the real show() slot
    24. connect( secWindow, &SecWindow::rejected, this, &QMainWindow::show );
    25.  
    26. // ...
    27. }
    28.  
    29. void MainWindow::showDialog()
    30. {
    31. secWindow->show(); // Reuses the member instance of SecWindow
    32. hide();
    33. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by d_stranz; 28th December 2018 at 18:11.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

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

    Kuushie118 (30th December 2018)

  7. #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: Qt Creator - Main widow and second window

    Quote Originally Posted by Kuushie118 View Post
    but sir, is there a way to fix my error?
    My professor wants me to do it that way

    you say that my error is that I don't have a class dialog.
    but don't I have one at secwindow.h line 23??
    Actually, yes you do have a declaration of Dialog at that location. I misread the error message. While you do have a Dialog class, you do not have a Dialog class constructor that accepts an argument. Your code:
    Qt Code:
    1. Dialog *dialog = new Dialog(this); //ERROR: no matching constructor for initialization of 'Dialog'
    To copy to clipboard, switch view to plain text mode 
    is passing an argument and this does not match any constructor you have declared:
    Qt Code:
    1. class Dialog : public QDialog
    2. {
    3. public:
    4. Dialog(); // <<<< no arguments accepted
    5. ...
    To copy to clipboard, switch view to plain text mode 
    Your Dialog constructor should have a similar declaration and implementation to the one you have for SecWindow.

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

    Kuushie118 (30th December 2018)

  9. #6
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Qt Creator - Main widow and second window

    Actually, yes you do have a declaration of Dialog at that location. I misread the error message.
    And I didn't scroll the code window far enough. That definition of a Dialog class looks like a failed / confused attempt to get something working. A class that contains a pointer to an instance of itself as a member variable?
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  10. The following user says thank you to d_stranz for this useful post:

    Kuushie118 (30th December 2018)

  11. #7
    Join Date
    Dec 2018
    Posts
    9
    Thanks
    7
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Qt Creator - Main widow and second window

    Thanks for all the help guys, it finally worked

Similar Threads

  1. Replies: 2
    Last Post: 8th November 2016, 05:44
  2. Resizeable main window in Qt Creator?
    By Lendrick in forum Qt Tools
    Replies: 2
    Last Post: 3rd June 2009, 20:57
  3. Replies: 11
    Last Post: 11th August 2008, 10:14
  4. Replies: 1
    Last Post: 7th November 2007, 09:39
  5. Replies: 15
    Last Post: 23rd March 2007, 17:16

Tags for this Thread

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.