QT常用UI操作代码

1.背景图:

 
class MainWindow : public QMainWindow
{
……
private:
    void paintEvent(QPaintEvent *event);
}

void MainWindow::paintEvent(QPaintEvent */*event*/)
{
    QString bgfile = ":/images/1024x768/bg.png";
    QPixmap pixmap = QPixmap(bgfile).scaled(this->size());
    QPainter painter(this);
    painter.drawPixmap(this->rect(), pixmap);
}
 

三态图片按钮:

QPushButton{border-image: url(:/images/c.png);} QPushButton:hover{border-image: url(:/images/c_p.png);} QPushButton:pressed{border-image: url(:/images/c_p.png);}

透明按钮(使用背景图中的按钮)

border-style:outset;border-radius:1px;border-color:beige;font:bold 4px;min-width:1em;padding:4px;

LineEdit透明

background:rgba(255,0,0,0);

RadioButton改变大小(圆圈)

QRadioButton::indicator { width: 39px; height: 39px; } QRadioButton::indicator::unchecked { image: url(:/images/radiobutton_unchecked.jpg); } QRadioButton::indicator:unchecked:hover { image: url(:/images/radiobutton_unchecked_hover.jpg); } QRadioButton::indicator:unchecked:pressed { image: url(:/images/radiobutton_unchecked_pressed.jpg); } QRadioButton::indicator::checked { image: url(:/images/radiobutton_checked.jpg); } QRadioButton::indicator:checked:hover { image: url(:/images/radiobutton_checked_hover.jpg); } QRadioButton::indicator:checked:pressed { image: url(:/images/radiobutton_checked_pressed.jpg); }");

改变字体大小颜色等

text-align: center; color:rgb(100,100,100,250);font-size:26px;font-weight:bold;font-family:Roman times;

隐藏鼠标:

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QApplication::setOverrideCursor(Qt::BlankCursor);
     ……
}

定时返回:

QTimer* _timerTimeout;
int _leftSeconds;

YourDialog::YourDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::YourDialog)
{
    _leftSeconds = 60;
    _timerTimeout = new QTimer();
    connect(_timerTimeout, SIGNAL(timeout()), this, SLOT(OnTimeout()));
    _timerTimeout->start(1000);

}

void YourDialog::OnTimeout()
{
    if(--_leftSeconds == 0)  reject();
    ui->label_Timer->setText(QString::number(_leftSeconds)+QString("s"));
}

连点屏幕左/右上角3次则弹出登陆界面

void mousePressEvent(QMouseEvent *event);

void MainWindow::mousePressEvent(QMouseEvent *event)
{
    int dx = event->x();
    int dy = event->y();
    if( dx <= 50 && dy <= 50)
    {
        static int hits = 0;
        static QDateTime lastHitTime;
        QDateTime now = QDateTime::currentDateTime();
        if(now.toTime_t() - lastHitTime.toTime_t() <=1)
        {
            hits++;
        }else{
            hits = 0;
        }
        lastHitTime = now;
        if(hits>=3)
        {
            hits = 0;
            OnBtnConfigClicked();
        }
    }
}