Results 1 to 20 of 20

Thread: custom slots / generated ui

  1. #1
    Join Date
    Sep 2010
    Posts
    21
    Thanks
    2
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Question custom slots / generated ui

    Hi all

    I have created a Qt application using MSVS 2010 with the Qt Add-In. I created the UI in designer and added two new slots for the QMainWindow (called generat0rXClass in my case). I have created two actions (exit_slot(), addScene2D_slot() ) which trigger the slots. Designer has generated the following code:

    Qt Code:
    1. QObject::connect(actionExit, SIGNAL(activated()), generat0rXClass, SLOT(exit_slot()));
    2. QObject::connect(actionNew_2D_Scene, SIGNAL(activated()), generat0rXClass, SLOT(addScene2D_slot()));
    To copy to clipboard, switch view to plain text mode 

    In my main class (generated by the add-In as well, called "generat0rX"

    like so:

    Qt Code:
    1. void exit_slot()
    2. {
    3. QMessageBox msgBox;
    4. msgBox.setText("SLOT: exit_slot triggered");
    5. msgBox.exec();
    6. }
    To copy to clipboard, switch view to plain text mode 

    But nothing happens (the click on the corresponding ui element does not trigger the slot). Why is that?

    Cheers,
    Michael

  2. #2
    Join Date
    Sep 2010
    Posts
    21
    Thanks
    2
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: custom slots / generated ui

    Sniffed around in the logs a bit and found the following errors:
    Qt Code:
    1. Object::connect: No such slot generat0rX::exit_slot() in E:\workspace\QTProjects\generat0rX\generat0rX\GeneratedFiles\ui_generat0rx.h:421
    2. Object::connect: (sender name: 'actionExit')
    3. Object::connect: (receiver name: 'generat0rXClass')
    4. Object::connect: No such slot generat0rX::addScene2D_slot() in E:\workspace\QTProjects\generat0rX\generat0rX\GeneratedFiles\ui_generat0rx.h:422
    5. Object::connect: (sender name: 'actionNew_2D_Scene')
    6. Object::connect: (receiver name: 'generat0rXClass')
    To copy to clipboard, switch view to plain text mode 

    Im a bit lost on this one - seems to be a namespace problem with the code (generated by the Add-In)

    Anyone?

  3. #3
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: custom slots / generated ui

    Where is the class definition for exit_slot() ?

  4. #4
    Join Date
    Sep 2010
    Posts
    21
    Thanks
    2
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: custom slots / generated ui

    in the generat0rx.h file:
    Qt Code:
    1. #ifndef GENERAT0RX_H
    2. #define GENERAT0RX_H
    3.  
    4. #include <QtGui/QMainWindow>
    5. #include "ui_generat0rx.h"
    6.  
    7. class generat0rX : public QMainWindow
    8. {
    9. Q_OBJECT
    10.  
    11. public:
    12. generat0rX(QWidget *parent = 0, Qt::WFlags flags = 0);
    13. ~generat0rX();
    14.  
    15. void exit_slot();
    16.  
    17. private:
    18. Ui::generat0rXClass ui;
    19.  
    20. };
    21.  
    22. #endif // GENERAT0RX_H
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: custom slots / generated ui

    There's no slots defined in that header file. You need at least something like "public slots:" for example to show which functions are slots.

    Secondly, this:

    Qt Code:
    1. void exit_slot()
    2. {
    3. QMessageBox msgBox;
    4. msgBox.setText("SLOT: exit_slot triggered");
    5. msgBox.exec();
    6. }
    To copy to clipboard, switch view to plain text mode 

    Isn't valid C++. This a C function.

  6. #6
    Join Date
    Sep 2010
    Posts
    21
    Thanks
    2
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: custom slots / generated ui

    Using the Designer, I was not really sure what needs to be done where.
    So when I try to add the "public slots:" in "generat0rX.h", I get a linker error that states something around
    Qt Code:
    1. "symbol not found in ui_generat0rX.h"
    To copy to clipboard, switch view to plain text mode 
    (I am not at my workstation, can post the exact linker error later). When I remove the "public slots:" statements, the code compiles again.

    I have created the two additional slots also in Designer (as part of generat0rXClass) by right-clicking on generat0rXClass and editing "Signals/Slots".

    On the "isn't valid c++": What is wrong with it? (btw: I copy/pasted it from a Nokia Qt tutorial )
    Last edited by mboeni; 13th October 2010 at 09:19.

  7. #7
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: custom slots / generated ui

    Once you have added slots to a header file, you must invoke the MOC to generate the appropriate dependancies. This is typically done by updating the project (In QtCreator I think is called "run qmake" or similar).

    Please post a link to the Nokia Qt tutorial.

  8. #8
    Join Date
    Sep 2010
    Posts
    21
    Thanks
    2
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: custom slots / generated ui

    I changed generat0rx.h to this:
    Qt Code:
    1. #ifndef GENERAT0RX_H
    2. #define GENERAT0RX_H
    3.  
    4. #include <QtGui/QMainWindow>
    5. #include "ui_generat0rx.h"
    6.  
    7. class generat0rX : public QMainWindow
    8. {
    9. Q_OBJECT
    10.  
    11. public:
    12. generat0rX(QWidget *parent = 0, Qt::WFlags flags = 0);
    13. ~generat0rX();
    14.  
    15. //void exit_slot();
    16. //void addScene2D_slot();
    17.  
    18. public slots:
    19. void exit_slot(void);
    20. void addScene2D_slot(void);
    21.  
    22.  
    23. private:
    24. Ui::generat0rXClass ui;
    25.  
    26. };
    27.  
    28. #endif // GENERAT0RX_H
    To copy to clipboard, switch view to plain text mode 

    which, when compiling gives me this output:
    Qt Code:
    1. 1>------ Build started: Project: generat0rX, Configuration: Debug Win32 ------
    2. 1> Moc'ing include\generat0rx.h...
    3. 1> moc_generat0rx.cpp
    4. 1> generat0rx.cpp
    5. 1> main.cpp
    6. 1> Generating Code...
    7. 1>moc_generat0rx.obj : error LNK2019: unresolved external symbol "public: void __thiscall generat0rX::addScene2D_slot(void)" (?addScene2D_slot@generat0rX@@QAEXXZ) referenced in function "public: virtual int __thiscall generat0rX::qt_metacall(enum QMetaObject::Call,int,void * *)" (?qt_metacall@generat0rX@@UAEHW4Call@QMetaObject@@HPAPAX@Z)
    8. 1>moc_generat0rx.obj : error LNK2019: unresolved external symbol "public: void __thiscall generat0rX::exit_slot(void)" (?exit_slot@generat0rX@@QAEXXZ) referenced in function "public: virtual int __thiscall generat0rX::qt_metacall(enum QMetaObject::Call,int,void * *)" (?qt_metacall@generat0rX@@UAEHW4Call@QMetaObject@@HPAPAX@Z)
    9. 1>E:\workspace\QTProjects\generat0rX\\generat0rX.exe : fatal error LNK1120: 2 unresolved externals
    10. ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
    To copy to clipboard, switch view to plain text mode 

    I pasted the full output so you see what the compiler (MSVS / QT Add-In in my case) does. It says that it does moc things as far as I can tell.

    Here is the link to the sample: http://doc.trolltech.com/4.7/qmessagebox.html

  9. #9
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: custom slots / generated ui

    Did have any CPP file with methods void generat0rX::exit_slot(void) and void generat0rX::addScene2D_slot(void) definitions ?

  10. #10
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: custom slots / generated ui

    By "not valid C++", I mean this:

    Qt Code:
    1. void exit_slot()
    2. {
    3. QMessageBox msgBox;
    4. msgBox.setText("SLOT: exit_slot triggered");
    5. msgBox.exec();
    6. }
    To copy to clipboard, switch view to plain text mode 

    to this:

    Qt Code:
    1. void generat0rX::exit_slot()
    2. {
    3. QMessageBox msgBox;
    4. msgBox.setText("SLOT: exit_slot triggered");
    5. msgBox.exec();
    6. }
    To copy to clipboard, switch view to plain text mode 

  11. #11
    Join Date
    Sep 2010
    Posts
    21
    Thanks
    2
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: custom slots / generated ui

    @Lesiok:

    Yes, the methods are defined here:
    Qt Code:
    1. #include "generat0rx.h"
    2. #include "qmessagebox.h"
    3.  
    4.  
    5. generat0rX::generat0rX(QWidget *parent, Qt::WFlags flags) : QMainWindow(parent, flags)
    6. {
    7. ui.setupUi(this);
    8.  
    9. }
    10.  
    11. generat0rX::~generat0rX()
    12. {
    13.  
    14. }
    15.  
    16.  
    17. void generat0rX::exit_slot()
    18. {
    19. QMessageBox msgBox;
    20. msgBox.setText("SLOT: exit_slot triggered");
    21. msgBox.exec();
    22.  
    23. //qApp->quit();
    24. //qApp->exit();
    25. }
    26.  
    27. void generat0rX::addScene2D_slot()
    28. {
    29. QMessageBox msgBox;
    30. msgBox.setText("SLOT: addScene2D triggered");
    31. msgBox.exec();
    32. }
    To copy to clipboard, switch view to plain text mode 

    Any idea whats wrong?

  12. #12
    Join Date
    Jan 2009
    Location
    The Netherlands and Spain
    Posts
    150
    Thanks
    6
    Thanked 18 Times in 18 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: custom slots / generated ui

    Quote Originally Posted by mboeni View Post
    ...
    [/CODE]
    But nothing happens (the click on the corresponding ui element does not trigger the slot). Why is that?
    ...
    What ui element should trigger the action?
    Did you connect the action to that element?
    How did you do that?

    Note that a slot can also be called without the use of a QAction. So, when you use a QPushButton, you can connect directly to the clicked() signal. No QAction involved.

  13. #13
    Join Date
    Sep 2010
    Posts
    21
    Thanks
    2
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: custom slots / generated ui

    @boudie: Here is what I did:
    1. I created a project in MSVS 2010 using the Add-In
    2. In Designer, I created my UI
    3. I created two actions: "actionExit" and "actionNew_2D_Scene" in the action editor
    4. I drag/dropped the two actions to a menubar and to a toolbar
    5. Then I added two custom slots (in the designer), named "exit_slot()" and "addScene2D_slot()" to "generat0rXClass" which is my QMainWindow
    6. Then I created two connections in the designer, which shows like follows in "ui_generat0rx.h":
    Qt Code:
    1. QObject::connect(actionExit, SIGNAL(activated()), generat0rXClass, SLOT(exit_slot()));
    2. QObject::connect(actionNew_2D_Scene, SIGNAL(activated()), generat0rXClass, SLOT(addScene2D_slot()));
    To copy to clipboard, switch view to plain text mode 

    I assumed that this should work - but I keep getting this error
    Qt Code:
    1. Object::connect: No such slot generat0rX::exit_slot() in E:\workspace\QTProjects\generat0rX\generat0rX\GeneratedFiles\ui_generat0rx.h:532
    2. Object::connect: (sender name: 'actionExit')
    3. Object::connect: (receiver name: 'generat0rXClass')
    To copy to clipboard, switch view to plain text mode 

    Please note: If I use a standard slot like "close()" of generat0rXClass - it works!

    So i must be missing something somewhere :s

  14. #14
    Join Date
    Sep 2010
    Posts
    21
    Thanks
    2
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: custom slots / generated ui

    Ok, I tried it the manual way also: I removed the two custom slots and the connection in designer and did it in code. My class now looks like this:

    generat0rx.h
    Qt Code:
    1. #ifndef GENERAT0RX_H
    2. #define GENERAT0RX_H
    3.  
    4. #include <QtGui/QMainWindow>
    5. #include "ui_generat0rx.h"
    6.  
    7. class generat0rX : public QMainWindow, private Ui::generat0rXClass
    8. {
    9. Q_OBJECT
    10.  
    11. public:
    12. generat0rX(QWidget *parent = 0, Qt::WFlags flags = 0);
    13. ~generat0rX();
    14.  
    15. public slots:
    16. void exit_slot(void);
    17. void addScene2D_slot(void);
    18.  
    19. private:
    20. Ui::generat0rXClass ui;
    21. };
    22.  
    23. #endif // GENERAT0RX_H
    To copy to clipboard, switch view to plain text mode 

    and generat0rx.cpp
    Qt Code:
    1. #include "generat0rx.h"
    2. #include "qmessagebox.h"
    3.  
    4.  
    5. generat0rX::generat0rX(QWidget *parent, Qt::WFlags flags) : QMainWindow(parent, flags)
    6. {
    7. ui.setupUi(this);
    8. QObject::connect(actionExit, SIGNAL(activated()), this, SLOT(exit_slot()));
    9. QObject::connect(actionNew_2D_Scene, SIGNAL(activated()), this, SLOT(addScene2D_slot()));
    10. }
    11.  
    12. generat0rX::~generat0rX()
    13. {
    14.  
    15. }
    16.  
    17. void generat0rX::exit_slot()
    18. {
    19. QMessageBox msgBox;
    20. msgBox.setText("SLOT: exit_slot triggered");
    21. msgBox.exec();
    22.  
    23. //qApp->quit();
    24. //qApp->exit();
    25. }
    26.  
    27. void generat0rX::addScene2D_slot()
    28. {
    29. QMessageBox msgBox;
    30. msgBox.setText("SLOT: addScene2D triggered");
    31. msgBox.exec();
    32. }
    To copy to clipboard, switch view to plain text mode 

    Now the above error is gone but I get a runtime exception:
    Qt Code:
    1. > QtCored4.dll!QObject::connect(const QObject * sender=0x00690000, const char * signal=0x00c5d294, const QObject * receiver=0x0026f5c0, const char * method=0x00c5d270, Qt::ConnectionType type=AutoConnection) Line 2483 + 0x8 bytes C++
    2. generat0rX.exe!generat0rX::generat0rX(QWidget * parent=0x00000000, QFlags<enum Qt::WindowType> flags={...}) Line 8 + 0x31 bytes C++
    3. generat0rX.exe!main(int argc=1, char * * argv=0x0069f9f0) Line 24 + 0x1d bytes C++
    4. generat0rX.exe!WinMain(HINSTANCE__ * instance=0x00980000, HINSTANCE__ * prevInstance=0x00000000, char * __formal=0x00072b9a, int cmdShow=1) Line 131 + 0x12 bytes C++
    5. generat0rX.exe!__tmainCRTStartup() Line 547 + 0x2c bytes C
    6. generat0rX.exe!WinMainCRTStartup() Line 371 C
    To copy to clipboard, switch view to plain text mode 

    and the debugger points to this line in "qobject.cpp":
    Qt Code:
    1. const QMetaObject *smeta = sender->metaObject();
    To copy to clipboard, switch view to plain text mode 

    Any ideas?

  15. #15
    Join Date
    Jan 2009
    Location
    The Netherlands and Spain
    Posts
    150
    Thanks
    6
    Thanked 18 Times in 18 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: custom slots / generated ui

    Your toolbar probably contains one or more QToolButtons. When such button is clicked, the clicked signal for that button is emitted. To react to the signal, you must have in the constructor of your class something like this:
    Qt Code:
    1. connect(myButton, SIGNAL(clicked()), this, SLOT(mySlot()));
    To copy to clipboard, switch view to plain text mode 
    Then every time the button is clicked, the code in function mySlot() is executed.

    In your GeneratorClass.h you have something like:
    Qt Code:
    1. private slots:
    2. void mySlot();
    To copy to clipboard, switch view to plain text mode 

    And.. please read the Qt-docs about signals and slots. It really helps...

  16. #16
    Join Date
    Oct 2010
    Posts
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: custom slots / generated ui

    Try the signal "triggered()" of your actions instead of "activated()":

    #
    QObject::connect(actionExit, SIGNAL(triggered()), this, SLOT(exit_slot()));
    #
    QObject::connect(actionNew_2D_Scene, SIGNAL(triggered()), this, SLOT(addScene2D_slot()));

  17. #17
    Join Date
    Sep 2010
    Posts
    21
    Thanks
    2
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: custom slots / generated ui

    Hi all

    First of all, thanks for the input! Alas, none of it worked.

    I tried something very simple and get exceptions too. So I assume the problem is rooted deeper. I have added a simple widget manipulation to generat0rx.cpp:
    Qt Code:
    1. #include "generat0rx.h"
    2. #include "qmessagebox.h"
    3. #include <QxtLogger>
    4. #include <QxtBasicFileLoggerEngine>
    5.  
    6. generat0rX::generat0rX(QWidget *parent, Qt::WFlags flags) : QMainWindow(parent, flags)
    7. {
    8. ui.setupUi(this);
    9. //QObject::connect(actionExit, SIGNAL(activated()), this, SLOT(exit_slot()));
    10. //QObject::connect(actionNew_2D_Scene, SIGNAL(activated()), this, SLOT(addScene2D_slot()));
    11.  
    12. //Start logging
    13. qxtLog->addLoggerEngine("FileLogger", new QxtBasicFileLoggerEngine("log/g0x.log"));
    14. qxtLog->info("Huhu INFO log entry!");
    15.  
    16.  
    17. outputTabTextEdit->append("TEST");
    18. }
    19.  
    20. generat0rX::~generat0rX()
    21. {
    22.  
    23. }
    24.  
    25. void generat0rX::exit_slot()
    26. {
    27. QMessageBox msgBox;
    28. msgBox.setText("SLOT: exit_slot triggered");
    29. msgBox.exec();
    30.  
    31. //qApp->quit();
    32. //qApp->exit();
    33. }
    34.  
    35. void generat0rX::addScene2D_slot()
    36. {
    37. QMessageBox msgBox;
    38. msgBox.setText("SLOT: addScene2D triggered");
    39. msgBox.exec();
    40. }
    To copy to clipboard, switch view to plain text mode 

    This line was added in the constructor:
    Qt Code:
    1. outputTabTextEdit->append("TEST");
    To copy to clipboard, switch view to plain text mode 

    and even THIS raises an exception. There must be something fundamentally wrong with the way I call ui elements...(I am using multiple inheritance btw). Just to make sure you get the full picture, here is generat0rx.h as well:

    Qt Code:
    1. #ifndef GENERAT0RX_H
    2. #define GENERAT0RX_H
    3.  
    4. #include <QtGui/QMainWindow>
    5. #include "ui_generat0rx.h"
    6.  
    7. class generat0rX : public QMainWindow, private Ui::generat0rXClass
    8. {
    9. Q_OBJECT
    10.  
    11. public:
    12. generat0rX(QWidget *parent = 0, Qt::WFlags flags = 0);
    13. ~generat0rX();
    14.  
    15. public slots:
    16. void exit_slot(void);
    17. void addScene2D_slot(void);
    18.  
    19. private:
    20. Ui::generat0rXClass ui;
    21. };
    22. #endif // GENERAT0RX_H
    To copy to clipboard, switch view to plain text mode 

    This is getting really frustrating :s

    Here is the call stack:
    Qt Code:
    1. > QtGuid4.dll!QTextEdit::append(const QString & text="TEST") Line 2617 + 0x7 bytes C++
    2. generat0rX.exe!generat0rX::generat0rX(QWidget * parent=0x00000000, QFlags<enum Qt::WindowType> flags={...}) Line 17 + 0x2e bytes C++
    3. generat0rX.exe!main(int argc=1, char * * argv=0x0079fab0) Line 17 + 0x1d bytes C++
    4. generat0rX.exe!WinMain(HINSTANCE__ * instance=0x01040000, HINSTANCE__ * prevInstance=0x00000000, char * __formal=0x000a2b9a, int cmdShow=1) Line 131 + 0x12 bytes C++
    5. generat0rX.exe!__tmainCRTStartup() Line 547 + 0x2c bytes C
    6. generat0rX.exe!WinMainCRTStartup() Line 371 C
    7. kernel32.dll!76e31194()
    8. [Frames below may be incorrect and/or missing, no symbols loaded for kernel32.dll]
    9. ntdll.dll!775eb495()
    10. ntdll.dll!775eb468()
    To copy to clipboard, switch view to plain text mode 

  18. #18
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: custom slots / generated ui

    You are mixing the different approaches of using ui files: See "Using a Designer UI File in Your Application" at the docs.
    Use
    Qt Code:
    1. setupUi(this);
    To copy to clipboard, switch view to plain text mode 
    instead of
    Qt Code:
    1. ui.setupUi(this);
    To copy to clipboard, switch view to plain text mode 
    and rerun qmake.

  19. The following user says thank you to Lykurg for this useful post:

    mboeni (18th October 2010)

  20. #19
    Join Date
    Sep 2010
    Posts
    21
    Thanks
    2
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: custom slots / generated ui

    Hi Lykurg

    I have read the article you mentioned, but missed the part about setupUI. Changing that as you proposed has solved my main issues. Thanks!

    Cheers,
    Michael

  21. #20
    Join Date
    Sep 2010
    Posts
    21
    Thanks
    2
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: custom slots / generated ui

    For all other newbies out there, I have put together a little tutorial on how to master the first few steps with Qt4 and MS Visual Studio using the Add-In.

    You find the tutorial here: Beginners Tutorial Qt 4.7 with Visual Studio and the Qt Add-In

    Please comment or PM me in the blog if you find any errors.

    All the best,
    Michael

Similar Threads

  1. Accessing generated pixmaps in html / Custom tooltips
    By Pieter from Belgium in forum Qt Programming
    Replies: 9
    Last Post: 1st August 2009, 15:24
  2. Creating custom slots
    By harb37 in forum Qt Programming
    Replies: 2
    Last Post: 23rd June 2008, 18:10
  3. Replies: 12
    Last Post: 23rd June 2008, 08:05
  4. Replies: 2
    Last Post: 12th July 2007, 09:55
  5. Replies: 16
    Last Post: 7th March 2006, 15:57

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.