• 9249阅读
  • 3回复

Qt样式表如何用? [复制链接]

上一主题 下一主题
 
只看楼主 倒序阅读 楼主  发表于: 2010-05-15
最近编了一个程序,想要自定义外观,但是却编译时出错了:
程序如下:
//我是在构造函数开始的时候照着《C++GUIQt4编程》上面的代码敲的,然后再后面定义按钮希望这些按钮就是前面说明的格式
#include <QStyle>
#include <Q3StyleSheet>
#include "Mainwindow.h"

class QPushButton;

MainWindow::MainWindow()
{
   //自定义按钮样式
    QPushButton
    {
        color: white;
        font: bold 10pt;
        border_image: url(:/images/button.png) 16;
        border_width: 16px;
        padding: -16px 0px;
        min_height: 32px;
        min_width: 60px;
    }
    //当鼠标指针位于上方
    QPushButton : hover
    {
        border_image: url(:/images/button_hover.png) 16;
    }
    //当鼠标指针按下时
    QPushButton : pressed
    {
        color: lightgray;
        border_image: url(:/images/button.png) 16;
        padding_top: -15px;
        padding_buttom: -17px;
    }

    //按钮定义
    roamButton = new QPushButton(tr("漫游"));    //漫游按钮
    payButton = new QPushButton(tr("付费"));     //付费按钮
    chatButton = new QPushButton(tr("聊天"));    //聊天按钮
    netButton = new QPushButton(tr("网络"));     //网络按钮
    intrButton = new QPushButton(tr("介绍"));    //介绍按钮
}
然后,编译就出错了,如下:
Mainwindow.cpp:2:24: error: Q3StyleSheet: No such file or directory
Mainwindow.cpp:15:23: error: invalid suffix "px" on integer constant
Mainwindow.cpp:17:21: error: invalid suffix "px" on integer constant
Mainwindow.cpp:18:20: error: invalid suffix "px" on integer constant
Mainwindow.cpp:30:23: error: invalid suffix "px" on integer constant
Mainwindow.cpp:31:26: error: invalid suffix "px" on integer constant
Mainwindow.cpp: In constructor 'MainWindow::MainWindow()':

错误说:px是常数后面无效的后缀。

Qt样式表是怎样用的吗?
该包含怎样的头文件?
麻烦各位给点提示,最好有点实例程序,谢谢!!
离线dbzhang800

只看该作者 1楼 发表于: 2010-05-15
多看看Qt自带的manual就好:
http://doc.qt.nokia.com/4.6/stylesheet-examples.html

Style Sheet Usage
Customizing the Foreground and Background Colors
Let's start by setting yellow as the background color of all QLineEdits in an application. This could be achieved like this:
  1. qApp->setStyleSheet("QLineEdit { background-color: yellow }");

If we want the property to apply only to the QLineEdits that are children (or grandchildren or grand-grandchildren) of a specific dialog, we would rather do this:
  1. myDialog->setStyleSheet("QLineEdit { background-color: yellow }");

If we want the property to apply only to one specific QLineEdit, we can give it a name using QObject::setObjectName() and use an ID Selector to refer to it:
  1. myDialog->setStyleSheet("QLineEdit#nameEdit { background-color: yellow }");

Alternatively, we can set the background-color property directly on the QLineEdit, omitting the selector:
  1. nameEdit->setStyleSheet("background-color: yellow");

To ensure a good contrast, we should also specify a suitable color for the text:
  1. nameEdit->setStyleSheet("color: blue; background-color: yellow");

It might be a good idea to change the colors used for selected text as well:
  1. nameEdit->setStyleSheet("color: blue;"
  2.                          "background-color: yellow;"
  3.                          "selection-color: yellow;"
  4.                          "selection-background-color: blue;");
只看该作者 2楼 发表于: 2010-05-16
谢谢啦,已经解决~~
离线wsszlj

只看该作者 3楼 发表于: 2010-07-15
楼主,请问你是如何解决的,如看到回复,请加QQ378645853,谢谢了,拜托哦。
快速回复
限100 字节
 
上一个 下一个