当按钮拥有焦点的时候,会有一个矩形点阵虚框。这在一个方形按钮上,倒不大引人注意。然而一个圆形按钮上,却显著起来。
这应如何去应对呢?
以下是出现此现象的代码示例:
- C/C++ code
#include <QApplication>#include <QPushButton>#include <QApplication>#include <QFile>#include <QStyleFactory>#include <QTextStream>bool setSkin(QApplication* const app, QString const &skinFile);int main(int argc, char *argv[]){ //加载应用程序实例 QApplication app(argc, argv); //加载主窗口 QWidget *widget = new QWidget(); widget->setFixedSize(350, 200); widget->move(0, 0); //加载PushButton QPushButton *button1 = new QPushButton("button1", widget); QPushButton *button2 = new QPushButton("button2", widget); button1->setFixedSize(100, 100); button2->setFixedSize(100, 100); button1->move(50, 50); button2->move(200, 50); //加载应用皮肤 setSkin(&app ,"skin.qss"); button1->setText(""); button2->setText(""); button1->setObjectName("MyPushButton"); button2->setObjectName("MyPushButton"); //显示主窗口 widget->showNormal(); //循环 return app.exec();}bool setSkin(QApplication* const app, QString const &skinFile){ QFile file(skinFile); if (QFile::exists(skinFile) && file.open(QIODevice::ReadOnly)) { QApplication::setStyle(QStyleFactory::create("Windows")); QString strTemp; QTextStream in(&file); while (!in.atEnd()) { strTemp.append(in.readLine()); } file.close(); app->setStyleSheet(strTemp); } else {#ifdef Q_WS_MAC qDebug("%s: %s: File does not exist %s... setting mac style...", __FILE__, __FUNCTION__, qPrintable(skinFile)); app->setStyle(new QMacStyle()); return true;#else qDebug("%s: %s: File does not exist or failed to open %s", __FILE__, __FUNCTION__, qPrintable(skinFile)); return false;#endif } return true;}
//skin.qss内容是:
- XML code
QPushButton{ color : red}*#MyPushButton{ min-width : 74px; min-height : 22px; border-image : url(./skin/images/pushbutton_normal.png); border-top : 2px transparent; border-bottom : 2px transparent; border-right : 2px transparent; border-left : 2px transparent;}
------解决方案--------------------