Q_object , signals , slots problem



  • hi everybody . im beginner in qt. i have some question. "exactly" what is Q_OBJECT and where i must write this macro and what does it do ?

    what is my problem in this code ?

    @#include <QtGui>

    class label : public QLabel
    {
    Q_OBJECT
    public:
    label(QWidget *parent = 0)
    : QLabel( parent )
    { }

    public:
    void setText(const QString &txt)
    {
    QLabel::setText( txt );
    emit valueChanged( txt.toInt() );
    }

    signals:
    void valueChanged(int);
    };

    class widget : public QWidget
    {
    Q_OBJECT
    public slots:
    void setValue(int t)
    {
    char buffer[100];
    itoa(t, buffer, 10);

        setWindowTitle( buffer );
    }
    

    };

    int main(int argc, char *argv[])
    {
    QApplication app(argc, argv);
    widget window;

    label l( &window );
    l.setText( "1" );
    QObject::connect(&l, SIGNAL(valueChanged(int)), &window, SLOT(setValue(int)));
    l.setText( "2" );
    
    window.show();
    return app.exec();
    

    }
    @

    i have a base problem with signal/slot please help me ! tnx a lot


  • Moderators

    Break your widget and label classes into their own files. It just works much better that way. Otherwise there is #include magic you have to do manually to include and link files that moc generates.

    The Q_OBJECT macro instructs the moc preprocessor to generate the code necessary to implement the signals and slots. It must appear in the private section of a class definition.

    Also, in your widget::setValue(int) method, use Qt's native string functions, rather than C-based code:
    @
    void setValue(int t)
    {
    QString buffer = QString::number(t);
    setWindowTitle( buffer );
    }
    @



  • why i must seprate header files ?


  • Moderators

    [quote author="ahura_24" date="1333867809"]why i must seprate header files ?[/quote]
    Your program is rather small at the time. So it might not be logic that you, but it is good practice. The reason is that it helps you to organize your code in a better way.

    in you particular issue mlong has provided the reason:
    [quote author="mlong" date="1333847455"]Break your widget and label classes into their own files. It just works much better that way. Otherwise there is #include magic you have to do manually to include and link files that moc generates.
    @[/quote]
    This should help you to get around some hurdles more easily.



  • Q_OBJECT, apart from being a trigger for the moc tool, also actualy declares a couple of methods on your object (that are mostly implemented by code generated by the moc tool) that are needed for the Qt meta object system and signals and slots to work properly.



  • [quote author="ahura_24" date="1333867809"]why i must seprate header files ?[/quote]

    Not a must, but a good practice.

    If you do not want to seprate and the name of this file is "main.cpp" and the build tool your chosen is "qmake" , you need to add such a line

    @
    #include "main.moc"
    @

    in this file.


Log in to reply
 

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