Hi
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")));
-
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.
pypmannetjies : This does not work. Did you mean to put the method signature in quotations? Thanks.Chris Cameron : Oh shit, sorry, I work in both Qt and PyQt and used the wrong syntax. Yes, definitely did not mean the quotes lol...pypmannetjies : Thanks for the help -
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"); }
-
Assuming you will have other
QPushButton
s that will cause other states to occur, you could put them inside aQButtonGroup
. 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 ; } }
-
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.
0 comments:
Post a Comment