Page 2 of 2 FirstFirst 12
Results 21 to 32 of 32

Thread: How to create custom slot in Qt Designer 4.1?

  1. #21
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 975 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default 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?

    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.

  2. #22
    Join Date
    Jan 2006
    Location
    Athens-Greece
    Posts
    15
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default 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?
    Qt Code:
    1. int main(int argc, char **argv)
    2. {
    3. QApplication app(argc, argv);
    4.  
    5. QDialog *window = new QDialog;
    6. Ui::bullshit _ui;
    7. _ui.setupUi(window);
    8.  
    9. window->show();
    10. QMessageBox::information(window, "startup msgbox",
    11. "The factory default will be used instead.");
    12. return app.exec();
    13. }
    To copy to clipboard, switch view to plain text mode 

    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.
    If there weren't noobs there would be no experts

  3. #23
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 975 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default 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:
    Qt Code:
    1. int main( int argc, char **argv )
    2. {
    3. QApplication app(argc, argv);
    4.  
    5. bullshit *window = new bullshit();
    6. window->show();
    7.  
    8. QMessageBox::information( window, "startup msgbox", "The factory default will be used instead." );
    9. return app.exec();
    10. }
    To copy to clipboard, switch view to plain text mode 

    PS. Please, next time choose a different name for your class.

  4. #24
    Join Date
    Jan 2006
    Location
    Athens-Greece
    Posts
    15
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to create custom slot in Qt Designer 4.1?

    Quote Originally Posted by jacek
    You don't instatiate your class anywhere.

    Try:
    Qt Code:
    1. int main( int argc, char **argv )
    2. {
    3. QApplication app(argc, argv);
    4.  
    5. mydialogclass *window = new mydialogclass();
    6. window->show();
    7.  
    8. QMessageBox::information( window, "startup msgbox", "The factory default will be used instead." );
    9. return app.exec();
    10. }
    To copy to clipboard, switch view to plain text mode 

    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.
    Last edited by cioannou; 16th January 2006 at 20:18.
    If there weren't noobs there would be no experts

  5. #25
    Join Date
    Jan 2006
    Location
    Athens-Greece
    Posts
    15
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to create custom slot in Qt Designer 4.1?

    I am sorry but it still doesn't work.

    If I connect the button to an existing slot e.g. accept() then the button works.


    Qt Code:
    1. #include <qapplication.h>
    2. #include <qdialog.h>
    3. #include <qmessagebox.h>
    4. #include "ui_MyDialog.h"
    5.  
    6. class MyDialog : public QDialog
    7. {
    8. public:
    9. MyDialog(QWidget *parent = 0);
    10. private:
    11. Ui::MyDialog ui;
    12. private slots:
    13. void msgbox();
    14. };
    15.  
    16. MyDialog::MyDialog( QWidget *parent )
    17. {
    18. ui.setupUi(this);
    19.  
    20. connect(ui.helloButton,SIGNAL(clicked()),this,SLOT(msgbox()));
    21. connect(ui.pressMe,SIGNAL(clicked()),this,SLOT(accept()));
    22. }
    23.  
    24. void MyDialog::msgbox()
    25. {
    26. QMessageBox::information(0, "test","message");
    27. }
    28.  
    29. int main(int argc, char **argv)
    30. {
    31. QApplication app(argc, argv);
    32. MyDialog *window=new MyDialog;
    33.  
    34. window->show();
    35. return app.exec();
    36. }
    To copy to clipboard, switch view to plain text mode 
    If there weren't noobs there would be no experts

  6. #26
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 975 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to create custom slot in Qt Designer 4.1?

    Quote Originally Posted by cioannou
    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.

    Qt Code:
    1. class MyDialog : public QDialog
    2. {
    3. Q_OBJECT
    4. public:
    5. ...
    6. };
    To copy to clipboard, switch view to plain text mode 

  7. #27
    Join Date
    Jan 2006
    Location
    Athens-Greece
    Posts
    15
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Thumbs up Re: How to create custom slot in Qt Designer 4.1?

    OK guys , I got it!!!!

    here it is:

    mydialog.h
    Qt Code:
    1. #include <qapplication.h>
    2. #include <qdialog.h>
    3. #include <qmessagebox.h>
    4. #include "ui_MyDialog.h" //This is the header generated by uic Mydialog.ui > ui_MyDialog.h
    5.  
    6. class MyDialog : public QDialog
    7. {
    8. Q_OBJECT
    9.  
    10. public:
    11. MyDialog(QWidget *parent = 0);
    12. private:
    13. Ui::MyDialog ui;
    14. private slots:
    15. void msgbox();
    16. };
    To copy to clipboard, switch view to plain text mode 

    mydialog.cpp
    Qt Code:
    1. #include "mydialog.h"
    2.  
    3. MyDialog::MyDialog( QWidget *parent )
    4. {
    5. ui.setupUi(this);
    6.  
    7. connect(ui.helloButton,SIGNAL(clicked()),this,SLOT(msgbox()));
    8. connect(ui.pressMe,SIGNAL(clicked()),this,SLOT(accept()));
    9. }
    10.  
    11. void MyDialog::msgbox()
    12. {
    13. QMessageBox::information(0, "test","message");
    14. }
    To copy to clipboard, switch view to plain text mode 

    main.cpp
    Qt Code:
    1. #include <qapplication.h>
    2. #include <qdialog.h>
    3. #include <qmessagebox.h>
    4. #include "mydialog.h"
    5.  
    6. int main(int argc, char **argv)
    7. {
    8. QApplication app(argc, argv);
    9. MyDialog *window=new MyDialog;
    10.  
    11. window->show();
    12. return app.exec();
    13. }
    To copy to clipboard, switch view to plain text mode 

    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!!!)
    If there weren't noobs there would be no experts

  8. #28
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 975 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default 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.

  9. #29
    Join Date
    Jan 2006
    Location
    Athens - Greece
    Posts
    219
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default 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

  10. #30
    Join Date
    Jan 2006
    Location
    Athens-Greece
    Posts
    15
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default 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
    If there weren't noobs there would be no experts

  11. #31
    Join Date
    Jan 2006
    Location
    Lincoln, NE USA
    Posts
    177
    Thanks
    3
    Thanked 7 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default 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.

    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"
    Qt Code:
    1. #ifndef DLGLOGIN_H
    2. #define DLGLOGIN_H
    3. #include "ui_dlglogin.h"
    4.  
    5. class dlgLogin : public QDialog
    6. {
    7. Q_OBJECT
    8.  
    9. public:
    10. Ui::dlgLoginUi dui;
    11. dlgLogin()
    12. {
    13. // called with 'dlgLogin dlg'. Only ID and password required
    14. dui.setupUi(this);
    15. dui.leUserName->setText("your revid");
    16. dui.leUserPassword->setText("");
    17. dui.leUserName->setFocus();
    18. dui.leUserName->selectAll();
    19. }
    20. };
    21.  
    22. #endif
    To copy to clipboard, switch view to plain text mode 

    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
    Qt Code:
    1. Ui::dlgLoginUi dui;
    To copy to clipboard, switch view to plain text mode 

    For completeness here is the main.cpp file that calls the login dialog before it executes the ht application

    Qt Code:
    1. int main( int argc, char * argv[] ) {
    2. QString strRejected = "";
    3. QApplication app(argc, argv);
    4. app.setQuitOnLastWindowClosed(false);
    5. dlgLogin dlg;
    6. if( dlg.exec() == QDialog::Accepted ){
    7. QSqlDatabase hapdb = QSqlDatabase::addDatabase(DBDRIVER);
    8. hapdb.setHostName(DBHOST);
    9. hapdb.setDatabaseName(DBNAME);
    10. hapdb.setUserName(dlg.dui.leUserName->text());
    11. hapdb.setPassword(dlg.dui.leUserPassword->text());
    12. if ( hapdb.open() ) {
    13. homestead ht;
    14. ht.RevID = dlg.dui.leUserName->text();
    15. ht.show();
    16. app.setQuitOnLastWindowClosed(true);
    17. return app.exec();
    18. } else {
    19. strRejected = QString("The Login was rejected because: %1").arg(hapdb.lastError().text()).toLatin1();
    20. QMessageBox::information(0,"Login Rejected!",strRejected,
    21. QMessageBox::Ok,QMessageBox::NoButton,QMessageBox::NoButton);
    22. return 1;
    23. }
    24. } else {
    25. strRejected = QString("User Canceled the login!").toLatin1();
    26. QMessageBox::information(0,"Login Rejected!",strRejected,
    27. QMessageBox::Ok,QMessageBox::NoButton,QMessageBox::NoButton);
    28. return 2;
    29. }
    30. }
    To copy to clipboard, switch view to plain text mode 


    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.
    dialogin_ui.jpg
    Last edited by GreyGeek; 18th January 2006 at 21:35.

  12. #32
    Join Date
    Jan 2006
    Location
    Lincoln, NE USA
    Posts
    177
    Thanks
    3
    Thanked 7 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default 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.

Similar Threads

  1. Custom signal in qt designer
    By txandi in forum Qt Tools
    Replies: 1
    Last Post: 4th December 2008, 21:25
  2. create a custom slot, hints?
    By pledians in forum Qt Programming
    Replies: 1
    Last Post: 2nd October 2008, 15:26
  3. How to create Custom Slot upon widgets
    By ashukla in forum Qt Programming
    Replies: 6
    Last Post: 8th September 2007, 15:07
  4. Replies: 2
    Last Post: 12th July 2007, 10:55
  5. custom slot + Designer
    By bashamehboob in forum Qt Tools
    Replies: 1
    Last Post: 28th April 2006, 16:17

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.