Results 1 to 9 of 9

Thread: To connect principal window with a second one

  1. #1
    Join Date
    Jan 2012
    Posts
    83
    Qt products
    Qt4
    Platforms
    Windows

    Default To connect principal window with a second one

    Greetings to all of you!
    I am facing a problem of connecting a parent window with a children one. I have successfully implemented each window as well as their respectives widgets, but the slots of the children window does not work.
    Here is my code:

    Qt Code:
    1. #ifndef DEF_ESSAIE
    2. #define DEF_ESSAIE
    3.  
    4. #include <QtGui>
    5.  
    6. class Essaie: public QMainWindow
    7. {
    8. Q_OBJECT
    9.  
    10. public:
    11. Essaie();
    12. ~Essaie();
    13.  
    14. private slots:
    15. void ouvrirFenetre1();
    16.  
    17.  
    18. private:
    19. QMainWindow *m_fenetrePrincipale;
    20. QPushButton *m_butonFenetre1;
    21.  
    22. };
    23. #endif // ESSAIE_H
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #ifndef DEF_FENETRE1
    2. #define DEF_FENETRE1
    3.  
    4. #include<QtGui>
    5.  
    6. class Fenetre1: public QMainWindow
    7. {
    8. Q_OBJECT
    9.  
    10. public:
    11. Fenetre1(QWidget *parent);
    12. ~Fenetre1();
    13.  
    14. private slots:
    15.  
    16. void displayList();
    17.  
    18. private:
    19. QWidget *m_fenetre1;
    20. QPushButton *m_butonFermer;
    21.  
    22. };
    23. #endif // FENETRE1_H
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #include "Essaie.h"
    2. #include "Fenetre1.h"
    3.  
    4.  
    5. Essaie::Essaie()
    6. {
    7. m_fenetrePrincipale = new QMainWindow;
    8. m_fenetrePrincipale->setFixedSize(300, 200);
    9.  
    10.  
    11. m_butonFenetre1 = new QPushButton("Ouvrir", this);
    12. m_butonFenetre1->move(75, 100);
    13.  
    14.  
    15. connect(m_butonFenetre1, SIGNAL(clicked()), this, SLOT(ouvrirFenetre1()));
    16.  
    17. }
    18.  
    19. void Essaie::ouvrirFenetre1()
    20. {
    21. Fenetre1 *fene = new Fenetre1(this);
    22. fene->show();
    23.  
    24. }
    25.  
    26. Essaie::~Essaie()
    27. {}
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #include "Fenetre1.h"
    2.  
    3. Fenetre1::Fenetre1(QWidget *parent = 0): QMainWindow(parent)
    4. {
    5. this->setFixedSize(200, 150);
    6. this->setWindowTitle("Fenetre1");
    7.  
    8. m_butonFermer = new QPushButton("Fermer", this);
    9.  
    10. connect(m_butonFermer, SIGNAL(clicked()), this, SLOT(close()));
    11. connect(m_butonDisplay, SIGNAL(clicked()), this, SLOT(displayList()));
    12.  
    13. }
    14.  
    15. Fenetre1::~Fenetre1()
    16. {}
    To copy to clipboard, switch view to plain text mode 

    On the children window, the closing slot is working properly, but not that of display.
    I would be greatfull to you if you could help me overcoming this problem.
    Many thanks in advance!
    Last edited by wysota; 27th March 2012 at 18:14.

  2. #2
    Join Date
    Feb 2011
    Posts
    6
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11

    Default Re: To connect principal window with a second one

    Hi,

    Could you please post code that actually compiles? And possibly, could you prefix the content of each file with the filename? It's a bit time-consuming to reconstruct the project by hand.
    I use sthg like this with inside a terminal:
    ( echo "#\!/bin/zsh"; for i in *.cpp *.h ; do echo "cat > $i <<EOF"; cat $i; echo "EOF"; echo; done ) > rebuild.sh
    (Don't play with this if you're not familiar with the shell; you're very likely to loose files..)
    I also think you should translate variables names and UI text in english to increase your probabiblity to get help on an english-speaking forum. I personally don't mind because french is my mother tongue, but imagine if the API of Qt was partly in Norwegian..
    It's more coherent to use english for all your method, class, variable names. BTW Essaie should probably be spelt Essai.

    connect(m_butonDisplay, SIGNAL(clicked()), this, SLOT(displayList()));
    1/Fenetre.cpp:11:13: error: ‘m_butonDisplay’ was not declared in this scope
    2/undefined reference to `Fenetre1::displayList()'

    I can help you debug programs, not concepts.

    Bye,

  3. #3
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,315
    Thanks
    314
    Thanked 870 Times in 857 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: To connect principal window with a second one

    I don't understand why you have two QMainWindow-derived classes, one as a child of the other. QMainWindow is meant to have a single child as its "central widget". I don't see that this code sets a central widget anywhere, just creates a new child QMainWindow with a button -somewhere- in it.

    French variable names aren't really the problem here, incorrect use of QMainWindow is.

  4. #4
    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: To connect principal window with a second one

    Stanfillirenfro. You need to look at how you are constructing your Essaie objects. Essaie is a QMainWindow but you are creating a new QMainWindow instance in the constructor. You then create a QPushButton that is parented to the Essaie object and don't use a layout.

    As for the specific issue with the m_butonDisplay button, you do not show any code to do with this button or the construction or display of an associated window. It may be a simple problem with the connect() call (you should see a message in your debug window if this is the case) , or somewhere else, we cannot tell.

    As an aside, I am a native English speaker with approximately 50 French words in my vocabulary but I don't have too much trouble with code in French unless extensive comments are required to understand what is going on.

  5. #5
    Join Date
    Jan 2012
    Posts
    83
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: To connect principal window with a second one

    I really appreciate your comments and help. Also for the langauge, I am really sorry! I will make sure in the future that english is used.
    Concerning my problem, I have revised deeply the code following your recommendations and I have make it clearer. Until now, I can't have any window after compilation. I received the following message: "The program ended abruptly".
    I would be greatfull to any of you if you help me solving this problem.

    Below is the new code with more modifications:

    MainWindow.h:
    Qt Code:
    1. #ifndef DEF_MAINWINDOW
    2. #define DEF_MAINWINDOW
    3.  
    4. #include <QtGui>
    5.  
    6. class MainWindow: public QMainWindow
    7. {
    8. Q_OBJECT
    9.  
    10.  
    11. public:
    12. MainWindow();
    13. ~MainWindow();
    14.  
    15. protected slots:
    16. void openFile();
    17. void saveFile();
    18. void codage();
    19. void openChildWindow();
    20.  
    21. private:
    22. QWidget *m_MainWindow;
    23. QPushButton *m_Exit;
    24. QRadioButton *m_codage;
    25. QLineEdit *m_openFile, *m_saveFile;
    26.  
    27.  
    28. };
    29. #endif
    To copy to clipboard, switch view to plain text mode 

    ChildWindow.h:
    Qt Code:
    1. #ifndef DEF_CHILDWINDOW
    2. #define DEF_CHILDWINDOW
    3.  
    4. #include<QtGui>
    5.  
    6. class ChildWindow: public QMainWindow
    7. {
    8. Q_OBJECT
    9.  
    10.  
    11. public:
    12. ChildWindow(QWidget *parent);
    13. ~ChildWindow();
    14.  
    15.  
    16. private:
    17. QPushButton m_exitChildWindow, *m_seqPur;
    18.  
    19.  
    20. };
    21. #endif // CHILDWINDOW_H
    To copy to clipboard, switch view to plain text mode 

    MainWindow.cpp:
    Qt Code:
    1. #include "MainWindow.h"
    2. #include "ChildWindow.h"
    3.  
    4.  
    5. MainWindow::MainWindow()
    6. {
    7. this->setFixedSize(500, 230);
    8. m_exit = new QpsuButton("Exit", this);
    9. m_codage = new QRadioButton(this);
    10. m_codage->move(50,120);
    11.  
    12. setCentralWidget(this);
    13.  
    14.  
    15. connect(m_exit, SIGNAL(triggered()), qApp, SLOT(quit()));
    16. connect(m_openFile, SIGNAL(clicked()), this, SLOT(openFile()));
    17. connect(m_saveFile, SIGNAL(clicked()), this, SLOT(saveFile()));
    18. connect(m_codage, SIGNAL(clicked()), this, SLOT(codage()));
    19.  
    20. }
    21.  
    22. void MainWindow::openFile()
    23. {
    24. QString file = QFileDialog::getOpenFileName(this, "Information", "Open", "*.text");
    25. m_openFile->setText(file);
    26. }
    27.  
    28. void MainWindow::saveFile()
    29. {
    30. QString file = QFileDialog::getSaveFileName(this, "Information", "Save");
    31. m_saveFile->setText(file);
    32. }
    33.  
    34.  
    35. void MainWindow::codage()
    36. {
    37.  
    38. QFile file1("myText1.txt");
    39. QFile file2("myText2.txt);
    40. QString line;
    41.  
    42. if(!file1.open(QIODevice::ReadOnly))
    43. {
    44. QMessageBox::critical(this, "Warning", "Open a file please");
    45. return;
    46. }
    47.  
    48. file2.open(QIODevice::WriteOnly);
    49. QTextStream out(&file2);
    50.  
    51.  
    52. line = line.remove(1, 5);
    53.  
    54.  
    55. out<< line<<"\r\n\r\n";
    56. }
    57.  
    58. void MainWindow::openChildWindow()
    59. {
    60. ChildWindow *fene = new ChildWindow(this);
    61. fene->show();
    62.  
    63. }
    64.  
    65. MainWindow::~MainWindow()
    To copy to clipboard, switch view to plain text mode 

    ChildWindow.cpp:
    Qt Code:
    1. #include "Fenetre1.h"
    2.  
    3. ChildWindow::ChildWindow(QWidget *parent = 0): QMainWindow(parent)
    4. {
    5.  
    6. m_exitChildWindow = new QPushButton("Exit", this);
    7. m_exitChildWindow->move(200, 220);
    8.  
    9. m_seqPur = new QPushButton;
    10. m_seqPur->move(175, 220);
    11.  
    12. resize(475, 250);
    13.  
    14.  
    15. connect(m_seqPur, SIGNAL(clicked()), this , SLOT(openChildWindow()));
    16. connect(m_exitChildWindow, SIGNAL(clicked()), this, SLOT(close()));
    17.  
    18.  
    19. }
    20.  
    21. ChildWindow::~ChildWindow()
    22. {}
    To copy to clipboard, switch view to plain text mode 

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

    Many thanks in advance!

  6. #6
    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: To connect principal window with a second one

    Did you copy and paste that code from a building program? It won't build as is.

    ChildWindow does not have a slot called openChildWindow(), so clicking on the m_seqPur button will not do anything.

    Please read Layout Management before you try to build anything more complicated. You will save yourself a lot of work and frustration.

  7. #7
    Join Date
    Jan 2012
    Posts
    83
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: To connect principal window with a second one

    ChrisW67, many thanks for your comments. I realise now that I had some errors in my code. The code was just part of a big one I am implementing now. I could not sent it all, otherwise it would be too big and could discourage the readers.
    Anyway, based on your many comments, I could overcome partially the problem. Now, I can compile the code in DEBUG mode, but not in relaese one. In release mode, when I have the macro Q_OBJECT in header's child file, such reply from the program is sent out: "undefined reference to `vtable for Childwindow" and the compilation fails. When I remove the macro Q_OBJECT, I have acces to all windows, but the slots on the Childwindow do not work.
    Could you please give me some advises to overcome this barrier?
    Many thanks in advance.

  8. #8
    Join Date
    Sep 2011
    Location
    Manchester
    Posts
    538
    Thanks
    3
    Thanked 106 Times in 103 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: To connect principal window with a second one

    When you add the Q_OBJECT macro to a class you have to re-run qmake.
    To be on the safe side just clear the whole project and build it again.
    This should fix the vtable error.

  9. #9
    Join Date
    Jan 2012
    Posts
    83
    Qt products
    Qt4
    Platforms
    Windows

    Post Re: To connect principal window with a second one

    Many thanks Spitfire and also to all of you for your help. By clearing the whole project and rebuilding it new, it compile now properly. The problems are solved.
    Many thanks one more time.

Similar Threads

  1. Replies: 6
    Last Post: 9th November 2011, 04:31
  2. connect window's close button
    By ahmetturan in forum Newbie
    Replies: 3
    Last Post: 12th July 2011, 07:50
  3. Replies: 2
    Last Post: 9th May 2011, 10:38
  4. Replies: 0
    Last Post: 8th March 2011, 22:08
  5. [PyQt4] Want to connect a window's close button
    By Dodle in forum Qt Programming
    Replies: 7
    Last Post: 21st April 2010, 11:13

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.