QPushButton icon aligned left with text centered

后端 未结 3 1191
悲&欢浪女
悲&欢浪女 2020-12-17 14:21

In my Qt 5.7.1 application I\'ve some buttons, I want to align button\'s icons to left and centre text, but there is no option in designer to do this.

I can

3条回答
  •  情书的邮戳
    2020-12-17 14:50

    Less code way without breaking UI style

    pushButton->setIcon(QApplication::style()->standardIcon(QStyle::SP_MessageBoxQuestion));
    pushButton->setStyleSheet("text-align:left;");
    pushButton->setLayout(new QGridLayout);
    
    QLabel* textLabel = new QLabel("Hello world!");
    textLabel->setAlignment(Qt::AlignRight | Qt::AlignVCenter); // or center
    textLabel->setAttribute(Qt::WA_TransparentForMouseEvents, true);
    
    pushButton->layout()->addWidget(textLabel);
    

    Remember to send setText signals to textLabel instead of pushButton

提交回复
热议问题