PDA

View Full Version : How to call a dialog from a mainwindow



luiz4um
18th May 2010, 20:27
I want to build an interface and I need to link a pushbutton in a mainwindow with a dialog. Both were made in Qt Designer. I tried some answers that I saw in others thread but I think it doesn't help me. So, here it goes my codes:
Mainwindow: telainicial (.h/.cpp)
Dialog: simples (.h/.cpp)

telainicial.h


#ifndef TELAINICIAL_H
#define TELAINICIAL_H

#include <QMainWindow>
#include "ui_telainicial.h"

class telainicial : public QMainWindow, public Ui::telainicial {
Q_OBJECT
public:
telainicial(QWidget *parent = 0);
private slots:
void telasimples();
};

#endif // TELAINICIAL_H


telainicial.cpp


#include <QtGui>
#include "telainicial.h"
#include "simples.h"

telainicial::telainicial(QWidget *parent) :
QMainWindow(parent)

{
setupUi(this);

connect(lentesimplesButton, SIGNAL(clicked()), this, SLOT(telasimples()));

}

void telainicial::telasimples(){

//code goes here

};


simples.h


#ifndef SIMPLES_H
#define SIMPLES_H

#include <QDialog>
#include "ui_simples.h"

class simples : public QDialog, public Ui::simples {
Q_OBJECT

public:
simples(QWidget *parent = 0);

};

#endif


simples.cpp


#include "simples.h"
#include <QtGui>

simples::simples(QWidget *parent) :
QDialog(parent)

{
setupUi(this);
}


The mains files are similiar like this:


#include <QtGui/QApplication>
#include "telainicial.h"

int main(int argc, char *argv[])
{
QApplication app(argc, argv);
telainicial mainwindow;
mainwindow.show();
return app.exec();
}


Can somebody help me?

luiz4um
18th May 2010, 20:42
I've tried:
simples dialog(this);
dialog.exec();

But it fails. The follow problem is detected:
undifined reference to 'simple::simple(QWidget*)'
undifined reference to 'vtable for simples'
undifined reference to 'vtable for simples'
collect2: Id returned 1 exit status

Someone knows what is it?

wysota
18th May 2010, 21:39
Run qmake and then build your program again. Btw. your class is called "simples" and you are calling the constructor as "simple".

luiz4um
19th May 2010, 23:19
My mistake. The follow problem was detected:
undifined reference to 'simples::simples(QWidget*)'

It's not a problem about compilation I think. I'm using the Qt Creator to do everything, like designer the ui files and compile the program. Is there any problem to do it?

wysota
20th May 2010, 09:15
Again, check if you actually implemented the simples constructor.

luiz4um
20th May 2010, 15:19
Yes, it works individually. But not when I call it by mainwindow. All my constructors are correct. I just don't know what to do... I've tried everything... Is it better to make the .pro file in the Qt Command Prompt? If you can run my codes on your pc (try to do it in Qt Creator)...

wysota
20th May 2010, 16:35
What "works individually"? The .pro file is not an issue here, the linker can't find the implementation of your constructor. Could you post the contents of the offending method?

luiz4um
20th May 2010, 17:41
I don't know exactly what are you asking for. I'm a beginner. Can I send my files to you by email? Sorry bother you, but it's important... :/

wysota
20th May 2010, 20:29
Please post the body of the constructor of class "simples". I hope you know what a constructor is... If not, you should first learn at least basics of C++ before taking on Qt.

luiz4um
26th May 2010, 19:30
I try to search in my files about C++ something connect to constructor class but I don't find a good answer. So, I again ask you with you can give me a example of constructor class. I'm still lost here. The following compile output appear when I run telainicial mainwindow:

g++ -enable-stdcall-fixup -Wl,-enable-auto-import -Wl,-enable-runtime-pseudo-reloc -mthreads -Wl -Wl,-subsystem,windows -o debug\TelaPrincipal.exe debug/main.o debug/telainicial.o debug/moc_telainicial.o -L"f:\QT\2010.02.1\qt\lib" -lopengl32 -lglu32 -lgdi32 -luser32 -lmingw32 -lqtmaind -lQtOpenGLd4 -lQtGuid4 -lQtCored4
mingw32-make[1]: Leaving directory `C:/Users/Luiz Paulo/Documents/TelaPrincipal'
mingw32-make: Leaving directory `C:/Users/Luiz Paulo/Documents/TelaPrincipal'
debug/telainicial.o:C:\Users\Luiz Paulo\Documents\TelaPrincipal/telainicial.cpp:16: undefined reference to `simples::simples(QWidget*)'
debug/telainicial.o: In function `~simples':
C:\Users\Luiz Paulo\Documents\TelaPrincipal/simples.h:7: undefined reference to `vtable for simples'
C:\Users\Luiz Paulo\Documents\TelaPrincipal/simples.h:7: undefined reference to `vtable for simples'
collect2: ld returned 1 exit status
mingw32-make[1]: *** [debug\TelaPrincipal.exe] Error 1
mingw32-make: *** [debug] Error 2
Exited with code 2.
Error while building project TelaPrincipal
When executing build step 'Make'

squidge
26th May 2010, 19:49
1) run qmake from the menu
2) rebuild your project
3) if it still doesn't work, post simples.h and simples.cpp if they are now different to your first post

luiz4um
26th May 2010, 19:51
It's already posted on top. Can you see? There are 4 codes... 2 of them are this codes...

wysota
26th May 2010, 20:00
My bet is you didn't add simples.cpp to SOURCES in your project file. At least I can't see simples.o being linked into the binary.

luiz4um
26th May 2010, 20:29
Can be... now I tried it but the compilation stop to work and when I check the compile output this message was shown:
Running build steps for project TelaPrincipal...
Starting: f:/qt/2010.02.1/qt/bin/qmake.exe C:/Users/Luiz Paulo/Documents/TelaPrincipal/TelaPrincipal.pro -spec win32-g++ -r
c:\Users\Luiz Paulo\Documents\TelaPrincipal\TelaPrincipal.pro:15 : Parse Error ('simples.cpp')
Error processing project file: C:/Users/Luiz Paulo/Documents/TelaPrincipal/TelaPrincipal.pro
Exited with code 3.
Error while building project TelaPrincipal
When executing build step 'QMake'

Just one doubt... Which simples's files have to be in the same folder that the mainwindow's folder? Cause when I made both, I made it individually, so each one have a specific folder. But, I copy and put the simples.h, simples.cpp, ui_simples.h in the mainwindow's folder...

wysota
26th May 2010, 23:19
Did you add simples.cpp to the SOURCES variable or did you just put it in the project file in a random place?

You can place the actual files anywhere you want as long as you tell the compiler where to find them.

luiz4um
27th May 2010, 14:00
How I tell the compiler where is the dialog files? Sorry the stupid question... I'm noob about Qt...

wysota
27th May 2010, 14:10
You need a proper project file. Could you post its contents?

luiz4um
27th May 2010, 14:16
The files .pro are below:

telainicial.pro:

#-------------------------------------------------
#
# Project created by QtCreator 2010-05-14T10:13:43
#
#-------------------------------------------------

QT += opengl

TARGET = TelaPrincipal
TEMPLATE = app


SOURCES += main.cpp\
telainicial.cpp
simples.cpp

HEADERS += telainicial.h

FORMS += telainicial.ui

simples.pro:

#-------------------------------------------------
#
# Project created by QtCreator 2010-05-14T10:47:49
#
#-------------------------------------------------

QT += opengl

TARGET = LenteSimples
TEMPLATE = app


SOURCES += main.cpp\
simples.cpp

HEADERS += simples.h

FORMS += simples.ui


Are this files you want to see?

wysota
27th May 2010, 14:26
Look at your first project file. simples.cpp is not part of the SOURCES variable.

luiz4um
27th May 2010, 14:39
It was missing a "\" after telainicial.cpp... I corrected it.
But where I put the local where the dialog (simples) is located?

wysota
27th May 2010, 14:54
Either provide the proper path in the SOURCES variable or use the DEPENDPATH variable.

luiz4um
27th May 2010, 15:19
Thanks!! Now it works!!! I'm so gratefull!

Now... using this post... Do you know how I custumize a pushbutton? Cause I'm programing in Windows and The default button is very common. I want to rounded it... make a shadow... something like this, you know...

wysota
27th May 2010, 17:10
Search the forum for similar questions. If you don't find an answer, start a new thread.

luiz4um
24th June 2010, 18:59
wysota,

Again, the similar problem appeared. Everything was working after I indexed corectly the .cpp file and .h file. But, suddently, when I edited a file in the mainwindow and I compiled it again, an error message showed up.

telainicial.cpp:3:21: error: simples.h: No such file or directory
telainicial.cpp:4:18: error: prog.h: No such file or directory
telainicial.cpp:5:19: error: trans.h: No such file or directory
telainicial.cpp:6:20: error: config.h: No such file or directory
telainicial.cpp: In member function 'void telainicial::telasimples()':
telainicial.cpp:21: error: 'simples' was not declared in this scope
telainicial.cpp:21: error: expected ';' before 'dlg'
telainicial.cpp:22: error: 'dlg' was not declared in this scope
telainicial.cpp: In member function 'void telainicial::telaprog()':
telainicial.cpp:26: error: 'prog' was not declared in this scope
telainicial.cpp:26: error: expected ';' before 'dlg'
telainicial.cpp:27: error: 'dlg' was not declared in this scope
telainicial.cpp: In member function 'void telainicial::telatrans()':
telainicial.cpp:31: error: 'trans' was not declared in this scope
telainicial.cpp:31: error: expected ';' before 'dlg'
telainicial.cpp:32: error: 'dlg' was not declared in this scope
telainicial.cpp: In member function 'void telainicial::telaconfig()':
telainicial.cpp:36: error: 'config' was not declared in this scope
telainicial.cpp:36: error: expected ';' before 'dlg'
telainicial.cpp:37: error: 'dlg' was not declared in this scope
mingw32-make[1]: Leaving directory `E:/Luiz Paulo/TelaPrincipal'
mingw32-make: Leaving directory `E:/Luiz Paulo/TelaPrincipal'
mingw32-make[1]: *** [debug/telainicial.o] Error 1
mingw32-make: *** [debug] Error 2
Exited with code 2.
Error while building project TelaPrincipal
When executing build step 'Make'


It looks the mainwindow doesn't find now the dialogs indexed on it. I already checked all located in .pro file and everything is correctly. I'm now confused...
I have a version that I saved on my flash that is working... but the codes are the same. I don't know what is going on...

Vit Stepanek
24th June 2010, 20:22
Post the file telainicial.cpp & h how they look currently. Did you change the location of your included files relatively to the "telainicial.cpp" file? Are they in a subfolder?
From the compiler output it appears that your header files were not found. Are you sure they are where they were last time you compiled it?
Check all of this. Something has changed certainly.

luiz4um
28th June 2010, 14:33
telainicial.cpp:



#include <QtGui>
#include "telainicial.h"
#include "simples.h"
#include "prog.h"
#include "trans.h"
#include "config.h"

telainicial::telainicial(QWidget *parent) :
QMainWindow(parent)

{
setupUi(this);

connect(lentesimples, SIGNAL(clicked()), this, SLOT(telasimples()));
connect(lenteprog, SIGNAL(clicked()), this, SLOT(telaprog()));
connect(transmissao, SIGNAL(clicked()), this, SLOT(telatrans()));
connect(configuracoes, SIGNAL(clicked()), this, SLOT(telaconfig()));
}

void telainicial::telasimples(){
simples dlg(this);
dlg.exec();
}

void telainicial::telaprog(){
prog dlg(this);
dlg.exec();
}

void telainicial::telatrans(){
trans dlg(this);
dlg.exec();
}

void telainicial::telaconfig(){
config dlg(this);
dlg.exec();

};


telainicial.h


#ifndef TELAINICIAL_H
#define TELAINICIAL_H

#include <QMainWindow>
#include "ui_telainicial.h"

class telainicial : public QMainWindow, public Ui::telainicial {
Q_OBJECT

public:
telainicial(QWidget *parent = 0);

private slots:
void telasimples();
void telaconfig();
void telaprog();
void telatrans();

};

#endif // TELAINICIAL_H


telainicial.pro

# -------------------------------------------------
# Project created by QtCreator 2010-05-14T10:13:43
# -------------------------------------------------
QT += opengl
TARGET = TelaPrincipal
TEMPLATE = app
SOURCES += main.cpp \
telainicial.cpp \
../Config/config.cpp \
../LenteSimples/simples.cpp \
../Progressiva/prog.cpp \
../Transmissao/trans.cpp
HEADERS += telainicial.h \
../Config/config.h \
../LenteSimples/simples.h \
../Progressiva/prog.h \
../Transmissao/trans.h
FORMS += telainicial.ui
OTHER_FILES +=


All dialog's folders are in the same folder, including the mainwindow's folder. I already have indexed the .h and .cpp files thounsand times but it just doesn't work. Can you try in your pc? I can send it by email...

Vit Stepanek
29th June 2010, 10:41
I see some files are in subfolders. Do you have an additional include directories set to these subfolders? Or try adding a subfolder name to the include command.
The compiler output is clear. It can't find all the files that are in subfolders. Focus on that.