styleSheet表是CSS2样式。在Qt里,可以简单就修改和调用。
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private slots:
void on_tabWidget_currentChanged(int index);
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
#include <QStyle>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
// ui->button1->setProperty("pagematches", true);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_tabWidget_currentChanged(int index)
{
qDebug() << "on tablewidget current changed";
// Set all buttons to false
ui->button1->setProperty("pagematches", false);
ui->button2->setProperty("pagematches", false);
ui->button3->setProperty("pagematches", false);
// Set one of the buttons to true
if (index == 0)
ui->button1->setProperty("pagematches", true);
else if (index == 1)
ui->button2->setProperty("pagematches", true);
else
ui->button3->setProperty("pagematches", true);
// Update buttons style
ui->button1->style()->polish(ui->button1);
ui->button2->style()->polish(ui->button2);
ui->button3->style()->polish(ui->button3);
}
styleSheet表如下:
QPushButton
{
color: white;
background-color: #27a9e3;
border-width: 0px;
border-radius: 3px;
}
QPushButton:hover
{
color: white;
background-color: #66c011;
border-width: 0px;
border-radius: 3px;
}
QPushButton:pressed
{
color: white;
background-color: yellow;
border-width: 0px;
border-radius: 3px;
}
QPushButton[pagematches=true]
{
color: white;
background-color: red;
border-width: 0px;
border-radius: 3px;
}
效果如下:
多谢,亲爱的美美。