Results 1 to 9 of 9

Thread: To connect principal window with a second one

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    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.

  2. #2
    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!

  3. #3
    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.

  4. #4
    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.

  5. #5
    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.

  6. #6
    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.