Re: How to create custom slot in Qt Designer 4.1?
Quote:
Originally Posted by cioannou
Thanks a lot , it compiled && linked now, but still not working
What do you mean by "not working"? How does your main() function look like?
Quote:
Can you please explain why we included the .moc file?
Because you have placed definition of a class with Q_OBJECT macro in .cpp file. You wouldn't have to do it, if that class definition wes in a header.
Re: How to create custom slot in Qt Designer 4.1?
Quote:
Originally Posted by jacek
What do you mean by "not working"? How does your main() function look like?
Code:
int main(int argc, char **argv)
{
Ui::bullshit _ui;
_ui.setupUi(window);
window->show();
"The factory default will be used instead.");
return app.exec();
}
I mean that I press my helloButton but no QMessageBox appears.
Quote:
Originally Posted by jacek
Because you have placed definition of a class with Q_OBJECT macro in .cpp file. You wouldn't have to do it, if that class definition wes in a header.
Got it, thanks a lot.
Re: How to create custom slot in Qt Designer 4.1?
Quote:
Originally Posted by cioannou
I mean that I press my helloButton but no QMessageBox appears.
You don't instatiate your class anywhere.
Try:
Code:
int main( int argc, char **argv )
{
bullshit *window = new bullshit();
window->show();
QMessageBox::information( window,
"startup msgbox",
"The factory default will be used instead." );
return app.exec();
}
PS. Please, next time choose a different name for your class.
Re: How to create custom slot in Qt Designer 4.1?
Quote:
Originally Posted by jacek
You don't instatiate your class anywhere.
Try:
Code:
int main( int argc, char **argv )
{
mydialogclass *window = new mydialogclass();
window->show();
QMessageBox::information( window,
"startup msgbox",
"The factory default will be used instead." );
return app.exec();
}
PS. Please, next time choose a different name for your class.
Ooops. This was really noob.
Sorry for the class name, it just came out from my dissapointment.
Already 'renamed' it.
Re: How to create custom slot in Qt Designer 4.1?
:confused: I am sorry but it still doesn't work.:confused:
If I connect the button to an existing slot e.g. accept() then the button works.
Code:
#include <qapplication.h>
#include <qdialog.h>
#include <qmessagebox.h>
#include "ui_MyDialog.h"
{
public:
private:
Ui::MyDialog ui;
private slots:
void msgbox();
};
MyDialog
::MyDialog( QWidget *parent
){
ui.setupUi(this);
connect(ui.helloButton,SIGNAL(clicked()),this,SLOT(msgbox()));
connect(ui.pressMe,SIGNAL(clicked()),this,SLOT(accept()));
}
void MyDialog::msgbox()
{
}
int main(int argc, char **argv)
{
MyDialog *window=new MyDialog;
window->show();
return app.exec();
}
Re: How to create custom slot in Qt Designer 4.1?
Quote:
Originally Posted by cioannou
:confused: I am sorry but it still doesn't work.
Because there is no Q_OBJECT macro, you need it if you define new signals or slots in your class.
Code:
{
Q_OBJECT
public:
...
};
Re: How to create custom slot in Qt Designer 4.1?
OK guys , I got it!!!! :)
here it is:
mydialog.h
Code:
#include <qapplication.h>
#include <qdialog.h>
#include <qmessagebox.h>
#include "ui_MyDialog.h" //This is the header generated by uic Mydialog.ui > ui_MyDialog.h
{
Q_OBJECT
public:
private:
Ui::MyDialog ui;
private slots:
void msgbox();
};
mydialog.cpp
Code:
#include "mydialog.h"
MyDialog
::MyDialog( QWidget *parent
){
ui.setupUi(this);
connect(ui.helloButton,SIGNAL(clicked()),this,SLOT(msgbox()));
connect(ui.pressMe,SIGNAL(clicked()),this,SLOT(accept()));
}
void MyDialog::msgbox()
{
}
main.cpp
Code:
#include <qapplication.h>
#include <qdialog.h>
#include <qmessagebox.h>
#include "mydialog.h"
int main(int argc, char **argv)
{
MyDialog *window=new MyDialog;
window->show();
return app.exec();
}
I still get an error concerning 'undefined reference to vtable for MyDialog' if I build inside Code::Blocks.
If I use:
qmake
mingw32-make
It works (finally!!!)
Re: How to create custom slot in Qt Designer 4.1?
Quote:
Originally Posted by cioannou
I still get an error concerning 'undefined reference to vtable for MyDialog' if I build inside Code::Blocks.
If I use:
qmake
mingw32-make
It works (finally!!!)
Every time you add or remove Q_OBJECT macro, you have to rerun qmake.
Re: How to create custom slot in Qt Designer 4.1?
Quote:
Originally Posted by cioannou
I still get an error concerning 'undefined reference to vtable for MyDialog' if I build inside Code::Blocks.
Create a custom tool that runs qmake in your project and on the project options select "Using a custom makefile". Then you only have to run your tool every time you add a new file to the project and then just build with the outputed makefile. It's the same as typing it but you get it with a couple of clicks ;)
Re: How to create custom slot in Qt Designer 4.1?
Thanks for the info, I will try it.
I have now added a listbox to my dialog.
Can you point me to some docs about how to get a reference to that listbox and e.g. add some items?
Txs
1 Attachment(s)
Re: How to create custom slot in Qt Designer 4.1?
Quote:
Originally Posted by Matt Smith
For learning Qt, there is a book called C++ GUI Programming with Qt 3, by J Blanchette & M Summerfield (Prentice Hall, 2004). It teaches all major aspects of Qt including the table and network modules. This might be a better step for a beginner than rushing straight into Qt 4 which so far is not well-documented other than for the reference documentation on the Trolltech website.
I first learned QT3 by using that book, and I really like the QT3 Designer, which was a "lite" GUI RAD for QT3. But, my biggest hurdle was trying to work with the way the Designer does things. And I ran into "generated code - do not modify" msgs just at the point that I needed to modify something to add my functionality. :(
Quote:
QtD4 is in my opinion a poor cousin for the Qt3 version, although some of its deficiencies have been made-up for in the 4.1 version - for example, there is a menu editor as there was in v3 and also an action editor. I still haven't found a way of adding actions to toolbars, though. And using Designer widgets in programs is nowhere near as simple as it was in Qt 3. For my project I'm using Qt 4.1 as by the time it's ready for use I suspect Qt and KDE 3 will both be obsolete, but Qt 3 right now is not obsolete and there may well be a greater body of experience out there in Qt 3 than Qt 4.
As much as I liked the QT3 Designer, making the QT4 Designer JUST a GUI designer and nothing more was, as I came to appreciate, an excellent move, both for me, as a newbie to C++ and QT, and for Trolltech, too. IMO.
First, the QT4 Designer just creates graphical interfaces. Nothing more. This simplifies both the design and use of the Designer. Trolltech can refocus resources to their central product, the QT widget set, and let others worry about a GUI RAD tool, or making the designer a plugin for such a tool. I can concentrate on the basics of C++ programming, and QT is just another API. If you've learned Java then QT will be a snap.
Second, I can apply in a more straight forward manner the basic techniques of OOP programming using C++ and QT's API, and this approach is MUCH MORE flexible than trying to stuff every application into the QT3 Designer straightjacket. It was the Designer's way or the hiway. :( Once I stopped trying to figure out which combobox in the lower right hand panel I needed to create a QSqlQuery in, and just created my own in a class definition in a header file I was time and money ahead of the old way. Adding dialogs, for example, is easy. One dialog.h file to define your dialog class, and one dialog.ui file to create the interface. After that it takes only a couple of lines to call the dialog in your app.cpp.
here is "dlglogin.h"
Code:
#ifndef DLGLOGIN_H
#define DLGLOGIN_H
#include "ui_dlglogin.h"
{
Q_OBJECT
public:
Ui::dlgLoginUi dui;
dlgLogin()
{
// called with 'dlgLogin dlg'. Only ID and password required
dui.setupUi(this);
dui.leUserName->setText("your revid");
dui.leUserPassword->setText("");
dui.leUserName->setFocus();
dui.leUserName->selectAll();
}
};
#endif
Below is a jpg of the dlglogin ui file that you can see shows the connections as well. The ObjectName of the dialog.ui is dlgLoginUi, which is why you see the line
Code:
Ui::dlgLoginUi dui;
For completeness here is the main.cpp file that calls the login dialog before it executes the ht application
Code:
int main( int argc, char * argv[] ) {
app.setQuitOnLastWindowClosed(false);
dlgLogin dlg;
if( dlg.
exec() == QDialog::Accepted ){ hapdb.setHostName(DBHOST);
hapdb.setDatabaseName(DBNAME);
hapdb.setUserName(dlg.dui.leUserName->text());
hapdb.setPassword(dlg.dui.leUserPassword->text());
if ( hapdb.open() ) {
homestead ht;
ht.RevID = dlg.dui.leUserName->text();
ht.show();
app.setQuitOnLastWindowClosed(true);
return app.exec();
} else {
strRejected
= QString("The Login was rejected because: %1").
arg(hapdb.
lastError().
text()).
toLatin1();
QMessageBox::information(0,
"Login Rejected!",strRejected,
return 1;
}
} else {
strRejected
= QString("User Canceled the login!").
toLatin1();
QMessageBox::information(0,
"Login Rejected!",strRejected,
return 2;
}
}
Lastly, just about everything I read about C++ programming applies to programming with QT as well. I can forget about using the designer to create anything but my *.ui files.:D
Attachment 47
Re: How to create custom slot in Qt Designer 4.1?
Quote:
Originally Posted by cioannou
That's exactly what I am trying to avoid.
If I change the app.h that is generated by the designer and afterwards add some new widgets to my dialog, all my private slots will be lost.
Am I missing something?
Yes. I am refering to app.h ... NOT app.ui or the ui_app.h file which the MOC creates.
The app.h file ISN'T generated by the designer. It's created by you!
In QT4 all the designer program does is create your ui interfaces. Any signal/slot connections you create with it apply only to the form itself or those objects that you placed on the form as you design your gui. You cannot use the designer to connect to a signal or slot which is not among those the designer adds itself to your ui. Ergo, you must create your own classes and add your external signals and slots to compliment the ui you design.
When I first switched to QT4 and left the QT3 Designer behind I was, at first, bewildered because I had learned how to make connections between objects, in the ui or out, strictly with the designer. It actually crippled me as far as learning how to OOP program using C++. Jacek was right. I am glad I hadn't burned the QT3 Designer habits as deeply into my 65 year old brain as long use of it would have done.