PDA

View Full Version : To connect principal window with a second one



Stanfillirenfro
27th March 2012, 14:28
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:


#ifndef DEF_ESSAIE
#define DEF_ESSAIE

#include <QtGui>

class Essaie: public QMainWindow
{
Q_OBJECT

public:
Essaie();
~Essaie();

private slots:
void ouvrirFenetre1();


private:
QMainWindow *m_fenetrePrincipale;
QPushButton *m_butonFenetre1;

};
#endif // ESSAIE_H


#ifndef DEF_FENETRE1
#define DEF_FENETRE1

#include<QtGui>

class Fenetre1: public QMainWindow
{
Q_OBJECT

public:
Fenetre1(QWidget *parent);
~Fenetre1();

private slots:

void displayList();

private:
QWidget *m_fenetre1;
QPushButton *m_butonFermer;

};
#endif // FENETRE1_H



#include "Essaie.h"
#include "Fenetre1.h"


Essaie::Essaie()
{
m_fenetrePrincipale = new QMainWindow;
m_fenetrePrincipale->setFixedSize(300, 200);


m_butonFenetre1 = new QPushButton("Ouvrir", this);
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()
{}


#include "Fenetre1.h"

Fenetre1::Fenetre1(QWidget *parent = 0): QMainWindow(parent)
{
this->setFixedSize(200, 150);
this->setWindowTitle("Fenetre1");

m_butonFermer = new QPushButton("Fermer", this);

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!

alecail
27th March 2012, 22:40
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,

d_stranz
27th March 2012, 23:41
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.

ChrisW67
27th March 2012, 23:53
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.

Stanfillirenfro
28th March 2012, 19:34
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:
#ifndef DEF_MAINWINDOW
#define DEF_MAINWINDOW

#include <QtGui>

class MainWindow: public QMainWindow
{
Q_OBJECT


public:
MainWindow();
~MainWindow();

protected slots:
void openFile();
void saveFile();
void codage();
void openChildWindow();

private:
QWidget *m_MainWindow;
QPushButton *m_Exit;
QRadioButton *m_codage;
QLineEdit *m_openFile, *m_saveFile;


};
#endif

ChildWindow.h:
#ifndef DEF_CHILDWINDOW
#define DEF_CHILDWINDOW

#include<QtGui>

class ChildWindow: public QMainWindow
{
Q_OBJECT


public:
ChildWindow(QWidget *parent);
~ChildWindow();


private:
QPushButton m_exitChildWindow, *m_seqPur;


};
#endif // CHILDWINDOW_H

MainWindow.cpp:
#include "MainWindow.h"
#include "ChildWindow.h"


MainWindow::MainWindow()
{
this->setFixedSize(500, 230);
m_exit = new QpsuButton("Exit", this);
m_codage = new QRadioButton(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()
{
QString file = QFileDialog::getOpenFileName(this, "Information", "Open", "*.text");
m_openFile->setText(file);
}

void MainWindow::saveFile()
{
QString file = QFileDialog::getSaveFileName(this, "Information", "Save");
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:
#include "Fenetre1.h"

ChildWindow::ChildWindow(QWidget *parent = 0): QMainWindow(parent)
{

m_exitChildWindow = new QPushButton("Exit", this);
m_exitChildWindow->move(200, 220);

m_seqPur = new QPushButton;
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:
#include <QApplication>
#include "MainWindow.h"


int main(int argc, char *argv[])
{
QApplication app(argc, argv);

MainWindow fenPr;

fenPr.show();

return app.exec();
}

Many thanks in advance!

ChrisW67
29th March 2012, 00:29
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.

Stanfillirenfro
29th March 2012, 10:39
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.

Spitfire
30th March 2012, 11:06
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.

Stanfillirenfro
30th March 2012, 20:19
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.