Connecting Actions to User defined Slots (I think)



  • Hello, I am new to Qt, and I am having problems trying wre and action to my own Slot.
    Here is my main Class

    #ifndef ALCHEMEDIA_H
    #define ALCHEMEDIA_H

    @#include <QtGui/QMainWindow>
    #include "ui_alchemedia.h"

    class alchemedia : public QMainWindow
    {
    // Q_OBJECT

    public:
    alchemedia(QWidget *parent = 0, Qt::WFlags flags = 0);
    ~alchemedia();

    protected:
    void OpenFile();

    private:
    void SetUpActions();
    Ui::Alchemedia m_ui;
    };

    #endif // ALCHEMEDIA_H@

    And this is my simple SetUpAction function
    @alchemedia::alchemedia(QWidget *parent, Qt::WFlags flags)
    : QMainWindow(parent, flags)
    {
    m_ui.setupUi(this);
    SetUpActions();
    }

    alchemedia::~alchemedia()
    {

    }

    void alchemedia::SetUpActions()
    {
    connect (m_ui.actionExit, SIGNAL (triggered(bool)), qApp, SLOT (quit()));

    connect (m_ui.actionOpen, SIGNAL (triggered(bool)), this, SLOT (OpenFile()));
    }

    void alchemedia::OpenFile()
    {

    }@
    However when I call
    connect (m_ui.actionOpen, SIGNAL (triggered(bool)), this, SLOT (OpenFile()));
    I get these error messages

    bq. Object::connect: No such slot QMainWindow::OpenFile() in ..\alchemedia.cpp:31
    Object::connect: (sender name: 'actionOpen')
    Object::connect: (receiver name: 'Alchemedia')

    Can someone please tell me what I need to do so that I can call me own slots.
    I am following the instruction form a Book name “The book of Qt4”
    But it does not say anything else about actions and user Slots.



  • Read the tutorial on signals and slots http://doc.qt.nokia.com/latest/signalsandslots.html



  • You must tell Qt that it is a slot. So write

    @
    protected slots:
     void OpenFile();
    @

    instead of

    @
     protected:
     void OpenFile();
    @

    Like in the samples of the page peppe pointed to.



  • Thank for the fast respond

    According that tutorial all I misses was adding macro Q_OBJECT to the class declaration.

    bq. All classes that contain signals or slots must mention Q_OBJECT at the top of their declaration. They must also derive (directly or indirectly) from QObject.

    The book also said that I must include that macro, although it did not explain the reason
    However, when I add that Macro to my project I get these errors in windows, I also get similar linking error in Linux as well.

    bq. 1>------ Build started: Project: alchemedia, Configuration: Debug Win32 ------
    1>Compiling...
    1>alchemedia.cpp
    1>Linking...
    1>alchemedia.obj : error LNK2001: unresolved external symbol "public: virtual struct QMetaObject const * __thiscall alchemedia::metaObject(void)const " (?metaObject@alchemedia@@UBEPBUQMetaObject@@XZ)
    1>alchemedia.obj : error LNK2001: unresolved external symbol "public: virtual void * __thiscall alchemedia::qt_metacast(char const *)" (?qt_metacast@alchemedia@@UAEPAXPBD@Z)
    1>alchemedia.obj : error LNK2001: unresolved external symbol "public: virtual int __thiscall alchemedia::qt_metacall(enum QMetaObject::Call,int,void * *)" (?qt_metacall@alchemedia@@UAEHW4Call@QMetaObject@@HPAPAX@Z)
    1>../Win32/alchemedia_d.exe : fatal error LNK1120: 3 unresolved externals
    1>Build log was saved at "file://c:\Newton_200\NewtonSDK\toolchain\alchemedia\project_2008\Win32\Debug\BuildLog.htm"
    1>alchemedia - 4 error(s), 0 warning(s)

    Is there a Library or somethong I need to add to the project
    These are the Qt libraries I am linking to:
    qtmain.lib QtCore4.lib QtGui4.lib QtOpenGL4.lib ….



  • @protected slots:
    void OpenFile(); @
    Oh yes, the book mentioned that too, I added those line already

    I beleive that and the Macro will solve it,
    However I get the link errors th aI can no figure out what I am missing



  • Clean your project, re-run qmake and build again, otherwise the tools do not catch up that you added Q_OBJECT to your class.



  • No, I do not think it is because not cleaning the project, I had done that many, many time.
    There have to be something else I am missing, because how can that happen in two different OS?

    Usually in other GUIs a macro like Q_OBJECT is a declaration and it is acompanied by some other macro wich is an Implemention,
    but I had not found anything like that in Qt.
    for what I can see:

    alchemedia::metaObject(void)const;
    ...

    are static functions that are missing in my class implementation, but I do not find information about it anywhere.



  • O I see, you mean running qmake to create a makefile.
    My impression was that QMake just created a makefile.
    Does this mean that the only way to make a QT project is by using Qmake?
    I cannot make a project using by hand using Visual studio?
    I am not using Qmake, I am using Qdesigner and Visual studio to make project manually.

    I will read about QMake then and see what I am missing.



  • Oh I see, I have to create a .pro file and use Qmake to create even my visual studio project,
    rather than me creating a VS project myselft.

    bq. The final step is to set the CONFIG variable. Since this is a Qt application, we need to put qt on the CONFIG line so that qmake will add the relevant libraries to be linked against and ensure that build lines for moc and uic are included in the generated Makefile.
    For Visual Studio users, qmake can also generate .dsp or .vcproj files, for example:
    qmake -tp vc -o hello.dsp hello.pro

    I will try that, thanks to the tip



  • If you want to work with MSVS, you can use the MSVS integration. That does all the job for MSVS, that qmake does it for all compilers on the command line.



  • Yes I know that. I tryied and it works great altought I did not try adding any user slots yet.
    Qt visual studio integration is great, but it does not works with Visual studio express and
    the large majority of my users are indies developers that mostly use free development tools like:

    Visual Studio Express, Linux, CodeBlock, and Xcode in that order.

    This is why I want t make the project using just Qt SDK, Qt designer and Visual Studio.
    I will try making a Qmake text project to create the Visual studio project, see how that goes.



  • [quote author="julio jerez" date="1296503159"]O I see, you mean running qmake to create a makefile.
    My impression was that QMake just created a makefile.
    Does this mean that the only way to make a QT project is by using Qmake?
    I cannot make a project using by hand using Visual studio?
    I am not using Qmake, I am using Qdesigner and Visual studio to make project manually.

    I will read about QMake then and see what I am missing.
    [/quote]

    qmake creates "only" the makefiles, that's true. But it also analyzes the sources and adds the steps for moc if a class contains the Q_OBJECT macro. The moc generated sources are automagically added to the Makefile an link steps etc. If Q_OBJECT is not present, qmake does not generate the moc steps. Thus, if you add Q_OBJECT later on, you must call qmake to catch up the change.



  • [quote author="julio jerez" date="1296508259"]Yes I know that. I tryied and it works great altought I did not try adding any user slots yet.
    Qt visual studio integration is great, but it does not works with Visual studio express and
    the large majority of my users are indies developers that mostly use free development tools like:

    Visual Studio Express, Linux, CodeBlock, and Xcode in that order.

    This is why I want t make the project using just Qt SDK, Qt designer and Visual Studio.
    I will try making a Qmake text project to create the Visual studio project, see how that goes.
    [/quote]

    Qt Creator works with all compiler toolchains (although debugging with the MSVS tools is still suboptimal) ... just a small hint ;-)



  • And QtCreator always calls qmake, so it creates the correct makefiles with the moc steps....



  • [quote author="julio jerez" date="1296508259"]...
    Qt visual studio integration is great, but it does not works with Visual studio express and
    the large majority of my users are indies developers that mostly use free development tools like:

    Visual Studio Express, Linux, CodeBlock, and Xcode in that order.

    This is why I want t make the project using just Qt SDK, Qt designer and Visual Studio.
    I will try making a Qmake text project to create the Visual studio project, see how that goes.

    [/quote]

    MSVS Express is free, that's right, but it's also not complete :-)
    The express edition does not support plug-ins. So If you want to use a free tool chain with Qt, I think it would be easier top use QtCreator than using MSVS free edition and always call qmake to create the vcproj files newly after adding a QObject derived class or adding Q_OBJECT macro to a class.



  • [quote author="Gerolf" date="1296548656"]And QtCreator always calls qmake, so it creates the correct makefiles with the moc steps....[/quote]

    No. Creator only calls qmake if the configuration (.pro and includes) has changed. Just adding Q_OBJECT to any class header does not trigger a qmake run (I just tested this). One can force a qmake run with the context menu on the project tree or from the menu bar, though.



  • Ok, I have no Creator here, thanks for the correction. But it's even much easier than with MSVS express, as you there have to recreate the vcproj and reload it to MSVS.



  • Ok I got it
    basically all I need to do is add a rule to each header file that include a Q_OBJECT macror
    I added this rule and it seems to work very nice

    ......\sdk\thirdParty\Qt\4.7.1\bin\moc -DUNICODE -DWIN32 ../$(InputName).h -o ../moc_$(InputName).cpp

    Thnak every one for the help.
    Qt seems very eassy to use and it is awesome.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.