PDA

View Full Version : What data type is the text in addAction(tr("&First Item"))



emp1953
24th March 2018, 23:00
This code comes from the grouppushbuttonbox example at
http://doc.qt.io/qt-5/qtwidgets-widgets-groupbox-example.html

At run time I want to build a string to replace the ("&First Item") with a concatenated integer. It fails to compile saying that std::string is in valid for the argument. QString fails for the same reason.
There has to be a way of changing this text at startup. Any help would be appreciated.

Thank You

emp1953

Added after 25 minutes:

Here is the compiler error

/home/123/groupbox_example/window.cpp:102: error: no matching function for call to 'Window::tr(std::string&)'
menu->addAction(tr(str));
^

ChristianEhrlicher
25th March 2018, 09:52
QObject::tr() takes a const char* as first argument: http://doc.qt.io/qt-5/qobject.html#tr

d_stranz
25th March 2018, 18:16
So this would work:



menu->addAction( tr( str.c_str() ) );


When you get a "no matching function call" compiler error it almost always means you have 1) spelled the name of the function incorrectly, 2) are trying to call the function with the wrong type of argument, or 3) are calling it with the wrong number of arguments.

The Qt documentation is always your friend.

emp1953
27th March 2018, 07:19
So this would work:



menu->addAction( tr( str.c_str() ) );


When you get a "no matching function call" compiler error it almost always means you have 1) spelled the name of the function incorrectly, 2) are trying to call the function with the wrong type of argument, or 3) are calling it with the wrong number of arguments.

The Qt documentation is always your friend.

Thank you d_stranz, it did work.