val
val

Reputation: 71

A template issue when creating a slot in Qt 4

I'm trying to create a class for signal/slot connection (old syntax, Qt 4.8) and I am doing something wrong as I keep receiving a template error: invalid declaration of member template in local class... That has obviously something to do with the Q_OBJECT macro... What should I do? Here is a modeled program:

#include <QtGui>
#include <QtCore>

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

  QWidget mw;
  mw.setWindowTitle("Main Window");
  mw.resize(400, 400);
  mw.show();

    QLabel label ("Enter something:", &mw);
    label.setAlignment(Qt::AlignHCenter);
    label.show();

    QLineEdit line (&mw);
    line.show();

    QString a = line.text();

    QTextEdit text (&mw);
    text.show();

    class MyObject : public QObject
    {
       Q_OBJECT       /* the problem is somewhere here... */

       public:
       QTextEdit text;
       QString a;

       public slots:
       void onClicked() {
          text.setText(a);
      }
    };

    QPushButton btn ("Convert", &mw);
    QObject::connect(
      &btn,
      SIGNAL(clicked()),
      this,
      SLOT(onClicked()));
    btn.show();

  QVBoxLayout layout_mw;

  layout_mw.addWidget(&label);
  layout_mw.addWidget(&line);
  layout_mw.addWidget(&btn);
  layout_mw.addWidget(&text);

  mw.setLayout(&layout_mw);

  return app.exec();

}

Upvotes: 2

Views: 63

Answers (1)

Qt's MOC can process neither nested classes nor local classes. You will have to move the class definition outside main. The documentation only mentions nested classes, but the limitation does apply to local classes too.

Upvotes: 3

Related Questions