pypmannetjies
pypmannetjies

Reputation: 31264

Qt4 existing slots are not recognized

I am currently trying to complete a project using Qt4 and C++. I am using buttons to switch between states. While trying to connect the buttons' clicked() signals to the textEdit to display the relevant state, I got stuck on an error:

Object::connect No such slot QTextEdit::append("move state") Object::connect No such slot QTextEdit::append("link state")

Only, QTextEdit definitely has an append(QString) slot.

Any ideas?

Some code samples:

QPushButton *move = new QPushButton("Move");
connect(move, SIGNAL(clicked()), textEdit, SLOT(append("move state")));

Upvotes: 0

Views: 1570

Answers (4)

Max Kueng
Max Kueng

Reputation:

Use a QSignalMapper to pass a hard-coded argument to the text edit's slot.

Example:

QSignalMapper* signalMapper = new QSignalMapper(this);
QPushButton* move = new QPushButton("Move");
signalMapper->setMapping(move, QString("move state"));
connect(move, SIGNAL(clicked()), signalMapper, SLOT(map()));
connect(signalMapper, SIGNAL(mapped(QString)), textEdit, SLOT(append(QString)));

Beware of the bugs in the above code.

Upvotes: 3

swongu
swongu

Reputation: 2299

Assuming you will have other QPushButtons that will cause other states to occur, you could put them inside a QButtonGroup. Then, you can use an enumeration, such as { MOVE_ID, STOP_ID, ... } to refer to the possible states.

QPushButton* move = new QPushButton( "Move" ) ;
QPushButton* stop = new QPushButton( "Stop" ) ;
QButtonGroup* buttonGroup = new QButtonGroup() ;
buttonGroup->addButton( move, MOVE_ID ) ;
buttonGroup->addButton( stop, STOP_ID ) ;

// Connecting QButtonGroup to writing function
connect( buttonGroup, SIGNAL( buttonClicked( int ) ),
         textEdit, SLOT( append( int ) ) ) ;

In textEdit, you'll define a function that appends the appropriate text depending on the state in which you get.

void append( int i )
{
   switch ( i )
   {
      case MOVE_ID:
         textEdit->append( "move state" ) ;
         break ;
      case STOP_ID:
         textEdit->append( "stop state" ) ;
         break ;
   }
}

Upvotes: 1

user44484
user44484

Reputation:

You can't pass in an argument (literally) to the append() slot when making a signal to slot connection.

You refer to it like a method signature:

SLOT(append(QString)) //or const QString, I'm not sure

If you need the textbox to append the words "move state" every time that button is clicked, then you should define your own slot that will do the append.

Upvotes: 6

McBeth
McBeth

Reputation: 1207

Chris has it in a nutshell.

That is one of the many reasons I like boost::signals a lot more (you are allowed to use boost::bind). You are basically going to need to create another function that captures the signal and then performs the append.

...

QPushButton *move = new QPushButton("Move");
connect(move, SIGNAL(clicked()), textEdit, SLOT(MoveState()));
}

...

void MyTextEdit::MoveState()
{
    append("move state");
}

Upvotes: 4

Related Questions