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:
Code:
#ifndef DEF_ESSAIE
#define DEF_ESSAIE
#include <QtGui>
{
Q_OBJECT
public:
Essaie();
~Essaie();
private slots:
void ouvrirFenetre1();
private:
};
#endif // ESSAIE_H
Code:
#ifndef DEF_FENETRE1
#define DEF_FENETRE1
#include<QtGui>
{
Q_OBJECT
public:
~Fenetre1();
private slots:
void displayList();
private:
};
#endif // FENETRE1_H
Code:
#include "Essaie.h"
#include "Fenetre1.h"
Essaie::Essaie()
{
m_fenetrePrincipale->setFixedSize(300, 200);
m_butonFenetre1->move(75, 100);
connect(m_butonFenetre1, SIGNAL(clicked()), this, SLOT(ouvrirFenetre1()));
}
void Essaie::ouvrirFenetre1()
{
Fenetre1 *fene = new Fenetre1(this);
fene->show();
}
Essaie::~Essaie()
{}
Code:
#include "Fenetre1.h"
{
this->setFixedSize(200, 150);
this->setWindowTitle("Fenetre1");
connect(m_butonFermer, SIGNAL(clicked()), this, SLOT(close()));
connect(m_butonDisplay, SIGNAL(clicked()), this, SLOT(displayList()));
}
Fenetre1::~Fenetre1()
{}
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!
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,
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.
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.
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:
Code:
#ifndef DEF_MAINWINDOW
#define DEF_MAINWINDOW
#include <QtGui>
{
Q_OBJECT
public:
MainWindow();
~MainWindow();
protected slots:
void openFile();
void saveFile();
void codage();
void openChildWindow();
private:
};
#endif
ChildWindow.h:
Code:
#ifndef DEF_CHILDWINDOW
#define DEF_CHILDWINDOW
#include<QtGui>
{
Q_OBJECT
public:
~ChildWindow();
private:
};
#endif // CHILDWINDOW_H
MainWindow.cpp:
Code:
#include "MainWindow.h"
#include "ChildWindow.h"
MainWindow::MainWindow()
{
this->setFixedSize(500, 230);
m_exit = new QpsuButton("Exit", this);
m_codage->move(50,120);
setCentralWidget(this);
connect(m_exit, SIGNAL(triggered()), qApp, SLOT(quit()));
connect(m_openFile, SIGNAL(clicked()), this, SLOT(openFile()));
connect(m_saveFile, SIGNAL(clicked()), this, SLOT(saveFile()));
connect(m_codage, SIGNAL(clicked()), this, SLOT(codage()));
}
void MainWindow::openFile()
{
m_openFile->setText(file);
}
void MainWindow::saveFile()
{
m_saveFile->setText(file);
}
void MainWindow::codage()
{
QFile file1
("myText1.txt");
QFile file2
("myText2.txt); QString line;
if(!file1.open(QIODevice::ReadOnly))
{
QMessageBox::critical(this, "Warning", "Open a file please");
return;
}
file2.open(QIODevice::WriteOnly);
QTextStream out(&file2);
line = line.remove(1, 5);
out<< line<<"\r\n\r\n";
}
void MainWindow::openChildWindow()
{
ChildWindow *fene = new ChildWindow(this);
fene->show();
}
MainWindow::~MainWindow()
ChildWindow.cpp:
Code:
#include "Fenetre1.h"
{
m_exitChildWindow->move(200, 220);
m_seqPur->move(175, 220);
resize(475, 250);
connect(m_seqPur, SIGNAL(clicked()), this , SLOT(openChildWindow()));
connect(m_exitChildWindow, SIGNAL(clicked()), this, SLOT(close()));
}
ChildWindow::~ChildWindow()
{}
Main.cpp:
Code:
#include <QApplication>
#include "MainWindow.h"
int main(int argc, char *argv[])
{
MainWindow fenPr;
fenPr.show();
return app.exec();
}
Many thanks in advance!
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.
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.
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.
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.