2011-12-10 146 views
2

我需要将两个图像添加到按钮!第一个图像必须是默认的,第二个图像点击它应该是活动的。 我已经试过这段代码,但它不能正常工作!如何使用CSS QT在按钮中添加两个图像?

QPushButton{background-image: url(:/images/activity.png);}  
QPushButton:pressed{background-image: url(:/images/activity_active.png);} 

任何人都请帮我分拣我犯的这个错误吗?

+0

不确定CSS是否有任何“已按下”选择器。你可以设置一个jsfiddle,以便我们可以看到并解决问题? – mowwwalker

+0

我不知道jsfiddle选项Walkerneo ...我也教了那个按不会西装..我看到这个网址:http://wiki.maemo.org/Customising_Qt_look_and_feel_and_Python_in_30_Mins#Add_Colours_and_Images .. Juz检查了这一点 – Naufal

+0

在qt我想我没有任何选择,。它的在线编辑权。 ,我不会合适..我们使用UI设计以及CSS – Naufal

回答

0

Qt附带的Style Sheet example包含了您需要了解的一切。在你的Qt SDK文件夹中找到类似QtSDK\Examples\4.7\widgets\stylesheet的东西。

更新:

当您运行示例程序中,你可以使用File菜单并选择Edit Style...编辑活动样式表。选择Pagefold风格。回到主屏幕,您会注意到OKCancel按钮在向上,向下和悬停状态下使用多个图像。

这些状态分别映射到QtSDK\Examples\4.7\widgets\stylesheet\images目录中的图像:pushbutton.png,pushbutton_pressed.pngpushbutton_hover.png

将这些图像映射到按钮的样式表是QtSDK\Examples\4.7\widgets\stylesheet\pagefold.qss。 Qt使用.qss作为样式表文件扩展名的不幸惯例可能会让您感到困惑。

在这里,你会发现下面的相关选择:

QPushButton, QComboBox[editable="false"], 
QComboBox[editable="true"]::drop-down { 
    border-image: url(:/images/pushbutton.png) 5; 
    border-width: 5; 
} 

QPushButton:hover, QComboBox[editable="false"]:hover, 
QComboBox[editable="true"]::drop-down:hover, QMenuBar::item:hover { 
    border-image: url(:/images/pushbutton_hover.png) 5; 
    border-width: 5; 
} 

QPushButton:pressed, QComboBox[editable="false"]:on, 
QComboBox[editable="true"]::drop-down:on, QMenuBar::item:on { 
    border-image: url(:/images/pushbutton_pressed.png) 5; 
    border-width: 5; 
} 

按钮图像使用路径前缀:/相当于存储在Qt的资源文件中的图像引用。将图像编译为资源不是必要的。它们也可以通过绝对或相对文件路径来识别。

要使这些样式表生效,需要将它们加载到QString中,并将其分配给应用程序对象或目标按钮的父级层次结构中的某个窗口小部件。在示例程序中,执行此操作的代码位于loadStyleSheet()方法中:

void StyleSheetEditor::loadStyleSheet(const QString &sheetName) 
{ 
    QFile file(":/qss/" + sheetName.toLower() + ".qss"); 
    file.open(QFile::ReadOnly); 
    QString styleSheet = QLatin1String(file.readAll()); 

    ui.styleTextEdit->setPlainText(styleSheet); 
    qApp->setStyleSheet(styleSheet); 
    ui.applyButton->setEnabled(false); 
} 
+0

有没有解决它!只有示例是在其中..我需要添加两个图像,并显示第一个默认和当按钮被按下时,它应该改变图像? – Naufal

+0

我不想知道stylesheet.the事实是我需要的图像。在这个QtSDK \ Examples \ 4.7 \ widgets \ stylesheet中没有解决方案。 – Naufal

+0

该示例包含您所要求的内容。 –