Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/147.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ Qt:图标弄乱了QPushButton的宽度-调整图标_C++_Qt_Qpushbutton_Qicon - Fatal编程技术网

C++ Qt:图标弄乱了QPushButton的宽度-调整图标

C++ Qt:图标弄乱了QPushButton的宽度-调整图标,c++,qt,qpushbutton,qicon,C++,Qt,Qpushbutton,Qicon,目前,我对动态创建的QPushButton使用以下样式表属性 string mystyle = "QPushButton { border: 1px solid #8f8f91; border-radius: 1px; min-width: 90px; min-height: 18px;}" QPushButton *button_test = new QPushButton( "Hello MyBig World!!" ,this); button_test->setStyleSh

目前,我对动态创建的QPushButton使用以下样式表属性

string mystyle =  "QPushButton { border: 1px solid #8f8f91;  border-radius: 1px; min-width: 90px; min-height: 18px;}"
QPushButton *button_test = new QPushButton( "Hello MyBig World!!" ,this);
button_test->setStyleSheet(mystyle.c_str());
button_test->setIcon(QIcon(":/../SomeFile.png"));
QPushButton旁边还有一个图标-QPushButton位于QToolBar内

当应用程序在我的计算机上运行时,这种样式可以正常工作。但是,当应用程序在另一个显示被放大的系统上运行时,问题就开始了。(在windows-7上,这是通过在控制面板中显示并选择较大的-150%来完成的)。当应用程序运行在带有放大显示的系统上时,这就是我得到的结果。(里面的文字应该是“你好,我的大世界!!”) 请注意,感叹号丢失了

我相信只有当按钮中有图标时才会出现此问题。没有图标,QpushButton看起来很好。这就是没有图标的情况。请注意,文本现在完全可见

这就是我创建QPushButton的方式

string mystyle =  "QPushButton { border: 1px solid #8f8f91;  border-radius: 1px; min-width: 90px; min-height: 18px;}"
QPushButton *button_test = new QPushButton( "Hello MyBig World!!" ,this);
button_test->setStyleSheet(mystyle.c_str());
button_test->setIcon(QIcon(":/../SomeFile.png"));
任何关于为什么会发生这种情况以及我如何解决这个问题的建议-按钮宽度在我的计算机上很好,所有的文本都与感叹号一起出现。但是,字体被放大的计算机上不会显示所有文本。为了在那台计算机上显示整个文本,我需要修改样式表的样式并增加最小宽度。我不想一直修改样式,而是想知道这个问题的正确和有效的解决方案

更新

这就是我现在使用的代码

std::string pbstyles = "QPushButton { border: 1px solid #8f8f91;  border-radius: 1px;  font: 12px;"
                       "background-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,stop: 0 #f6f7fa, stop: 1 #dadbde); "
                       "padding-right:50px;padding-left:50px;height:25px; }"
                       "QPushButton:pressed { background-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,stop: 0 #dadbde, stop: 1 #f6f7fa); }"
                       "QPushButton:hover{background-color: qlineargradient(spread:pad, x1:0, y1:0.0568182, x2:1, y2:0.0454545, stop:0 rgba(85, 170, 255, 255), stop:1 rgba(255, 255, 255, 255));}";


std::string ArrowStyle = "QPushButton::menu-indicator {/*background-color: darkblue ;*/ subcontrol-origin: padding; margin-right: -60px}";
    pbstyles+=ArrowStyle;


QMenu *menu = new QMenu("This is a button",this);
QPushButton *button = new QPushButton( menu->title(),this);
button->setMenu(menu_contacts);
button->setStyleSheet(pbstyles.c_str());
button->setIcon(QIcon(":/someicon.ico"));
button->setIconSize(QSize(16, 16));
ui.toolBar->addWidget(button_contacts);
这就是我得到的(注意文本仍然被切碎:(!!!!)


在我的测试中,出现这种情况是因为按钮的图标和文本被放大了,但按钮没有放大。要解决此问题,您可以使用以下方法:

QString stylesheet = ...font: 12px;... //Or another size that you expect the button text must have.
以及:


这些设置应该保证按钮的图标和文本在放大和未放大的桌面上具有相同的大小。

刚刚更新了我的帖子-你的方法并不能真正解决问题