Qt4之改变窗体显示风格

可以利用QStyleFactory获得当前系统支持的窗体风格,

也可以调用QApplication::setStyle()改变窗体的风格。


本实例通过一个显示风格可变窗体,通过下拉列表框中的选择,改变窗体的显示风格。

代码如下所示:

style.h:

#ifndef STYLE_H
#define STYLE_H

#include <QDialog>

class QFrame;

class Style : public QDialog
{
    Q_OBJECT
public:
    Style(QWidget *parent=0);
    
    void createLeftLayout();
    void createRightLayout();
    
public slots:
    void slotChangeStyle(QString);
    
private:
    QFrame *leftFrame;
    QFrame *rightFrame;
};

#endif  // STYLE_H

style.cpp:

#include "style.h"

#include <QtGui>

Style::Style(QWidget *parent)
	:QDialog(parent)
{
    setWindowTitle(tr("Change Window Style"));
    
    createLeftLayout();
    createRightLayout();
    
    QHBoxLayout *mainLayout = new QHBoxLayout;
    mainLayout->setMargin(10);
    mainLayout->setSpacing(5);
    mainLayout->addWidget(leftFrame);
    mainLayout->addWidget(rightFrame);
    setLayout(mainLayout);
}

void
Style::createLeftLayout()
{    
    leftFrame = new QFrame;
    leftFrame->setFrameStyle(QFrame::Panel|QFrame::Raised);
    QComboBox *styleComboBox = new QComboBox;
    styleComboBox->addItems(QStyleFactory::keys());

    QPushButton *button = new QPushButton(tr("pushbutton"));
    QHBoxLayout *hbox = new QHBoxLayout;
    hbox->addWidget(styleComboBox);
    hbox->addWidget(button);
    
    QSpinBox *spinBox = new QSpinBox;
    spinBox->setRange(0,9);
    QLineEdit *lineEdit = new QLineEdit;
    lineEdit->setText("Hello Hello");
    
    QGroupBox *group = new QGroupBox;
    QRadioButton *radio1 = new QRadioButton(tr("radiobutton1"),group);
    QRadioButton *radio2 = new QRadioButton(tr("radiobutton2"),group);
    QRadioButton *radio3 = new QRadioButton(tr("radiobutton3"),group);
    QCheckBox *checkBox = new QCheckBox(tr("checkbox"),group);
    QVBoxLayout *groupLayout = new QVBoxLayout;
    groupLayout->addWidget(radio1);
    groupLayout->addWidget(radio2);
    groupLayout->addWidget(radio3);
    groupLayout->addWidget(checkBox);
    group->setLayout(groupLayout);
    
    QVBoxLayout *vbox = new QVBoxLayout;
    vbox->addLayout(hbox);
    vbox->addWidget(spinBox);
    vbox->addWidget(lineEdit);
    vbox->addWidget(group);
    
    leftFrame->setLayout(vbox);

    connect(styleComboBox,SIGNAL(activated(QString)),this,SLOT(slotChangeStyle(QString)));
    slotChangeStyle(QStyleFactory::keys()[0]);
}

void
Style::createRightLayout()
{
    rightFrame = new QFrame;
    rightFrame->setFrameStyle(QFrame::Panel|QFrame::Raised);
    
    QSlider * slider = new QSlider(Qt::Horizontal);
    QTableWidget *table = new QTableWidget;
    table->setColumnCount(3);
    table->setRowCount(4);
    QTextEdit *edit = new QTextEdit;
    edit->setText("Hello World!\n Qt by Trolltech is the standard framework for developing high-performance cross-platform applications.Qt provides single-source portability across Microsoft Windows, Mac OS X, Linux, all major commercial Unix variants, and embedded Linux. On embedded Linux, the Qt API is available as Qtopia Core.");
    QVBoxLayout *layout = new QVBoxLayout;
    layout->setSpacing(10);
    layout->addWidget(slider);
    layout->addWidget(table);
    layout->addWidget(edit);
    
    rightFrame->setLayout(layout);
}

void
Style::slotChangeStyle(QString style)
{

    QApplication::setStyle(QStyleFactory::create(style));
    QApplication::setPalette(QApplication::style()->standardPalette());
}



main.cpp:

#include <QApplication>
#include "style.h"

int
main(int argc, char * argv[])
{
    QApplication app(argc,argv);
    Style s;
    s.show();
    return app.exec();
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值