Qt - QPushButton 图标边距需要保持不变
我使用下面的函数创建一组按钮,其中包含可以更改宽度的文本“buttonName”。当我单击其中一个按钮时出现的图标会根据文本的宽度自行调整;如何使图标与按钮右侧的边距保持相同,无论文本如何?不要说自定义委托,因为我还不知道如何实现它!
QPushButton *LayoutCreator::createButton(const QString &buttonName) {
QIcon ico;
ico.addPixmap(QPixmap(":images/images/on.png"), QIcon::Normal, QIcon::On);
ico.addPixmap(QPixmap(":images/images/off.png"), QIcon::Normal, QIcon::Off);
QPushButton* button = new QPushButton(buttonName);
button->setStyleSheet("QPushButton { height: 70px; font-size: 20px; }");
button->setIcon(ico);
button->setLayoutDirection(Qt::RightToLeft);
button->setIconSize(QSize(32,32));
button->setCheckable(true);
return button;
}
I create a set of buttons using the function below with text, buttonName, that can change in width. The icon that appears when I click one of the buttons then justfies itself based on the width of the text; how do I make the icon stay the same margin from the right of the button regardless of text? Don't say custom delegate, because I haven't been able to figure out how to implement that!
QPushButton *LayoutCreator::createButton(const QString &buttonName) {
QIcon ico;
ico.addPixmap(QPixmap(":images/images/on.png"), QIcon::Normal, QIcon::On);
ico.addPixmap(QPixmap(":images/images/off.png"), QIcon::Normal, QIcon::Off);
QPushButton* button = new QPushButton(buttonName);
button->setStyleSheet("QPushButton { height: 70px; font-size: 20px; }");
button->setIcon(ico);
button->setLayoutDirection(Qt::RightToLeft);
button->setIconSize(QSize(32,32));
button->setCheckable(true);
return button;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试添加
到您的按钮样式表中。
Try adding
to your button style sheet.