Reputation: 103
I have a QWidget
which I have attached to the toolbar of a QMainWindow
. I would like to change the color of the pushbutton text when the buttons are activated. I realize that I could create a method for each pushbutton (example below), but I am wondering if I could create one method that uses the name of the desired pushbutton (pseudocode below).
Change Specific pushbutton:
void ToolBarClass::changeOKbutton(QColor color)
{
ui->pushbutton_ok->[however text color is changed](color);
}
Change variable pushbutton
void ToolBarClass::changePushButton(QString buttonName, QColor color)
{
ui->[accessUImemberByName](buttonName)->[however text color is changed](color);
}
This is not a duplicate of this question because I am looking for Qt specific functionality to access ui members by name, which appears to be very different from the "duplicate" question.
Upvotes: 2
Views: 329
Reputation: 2143
You can use QObject::objectName()
function, like below,
void ToolBarClass::changePushButton(QString buttonName, QColor color)
{
QList< QPushButton* > listBtnAll = findChildren< QPushButton* >();
for ( int i = 0; i < listBtnAll.size(); i++ )
{
if ( listBtnAll[ i ]->objectName() == buttonName )
{
listBtnAll[ i ]->[however text color is changed](color);
break;
}
}
}
As @Pie_Jesu's advice, I've changed code, like below,
void ToolBarClass::changePushButton(QString buttonName, QColor color)
{
QPushButton *pButton = findChild<QPushButton *>( buttonName );
if ( pButton )
{
pButton->[however text color is changed](color);
}
}
I think the cost of search time is same, however, the cost of typing is more efficient than old code.
Upvotes: 3