简单的QT示例

目录

一、简述

二、代码

一、dialog.h

二、dialog.cpp

三、main.cpp

三、运行演示


一、简述

刚学qt,写了一个简单的qt例子。在图形中有:东西南北中五个按钮,按东图形会向右移动10个像素,以此类推,按中会复原。

二、代码

一、dialog.h

#ifndef DIALOG_H
#define DIALOG_H

#include <QDialog>
#include <QPushButton>
#include <QDebug>
#define  QPushButton_STYTLE_Middle  (QString("\
/*按钮普通态*/\
QPushButton\
{\
    font-family:Microsoft Yahei;\
    /*字体大小为20点*/\
    font-size:20pt;\
    /*字体颜色为白色*/\
    color:red;\
    /*背景颜色*/\
    background-color:rgb(255,255,0);\
    /*边框圆角半径为8像素*/\
    border-radius:8px;\
}\
/*按钮悬停态*/\
QPushButton:hover\
{\
    /*背景颜色*/\
    background-color:rgb(255, 102, 0);\
}\
/*按钮按下态*/\
QPushButton:pressed\
{\
    /*背景颜色*/\
    background-color:rgb(255, 239, 213);\
    /*左内边距为3像素,让按下时字向右移动3像素*/\
    padding-left:3px;\
    /*上内边距为3像素,让按下时字向下移动3像素*/\
    padding-top:3px;\
}"))
#define  QPushButton_STYTLE_East (QString("\
/*按钮普通态*/\
QPushButton\
{\
    font-family:Microsoft Yahei;\
    /*字体大小为20点*/\
    font-size:20pt;\
    /*字体颜色为白色*/\
    color:white;\
    /*背景颜色*/\
    background-color:rgb(0, 128, 128);\
    /*边框圆角半径为8像素*/\
    border-radius:8px;\
}\
/*按钮悬停态*/\
QPushButton:hover\
{\
    /*背景颜色*/\
    background-color:rgb(204, 0, 255);\
}\
/*按钮按下态*/\
QPushButton:pressed\
{\
    /*背景颜色*/\
    background-color:rgb(255, 239, 213);\
    /*左内边距为3像素,让按下时字向右移动3像素*/\
    padding-left:3px;\
    /*上内边距为3像素,让按下时字向下移动3像素*/\
    padding-top:3px;\
}"))

#define  QPushButton_STYTLE_West (QString("\
/*按钮普通态*/\
QPushButton\
{\
    font-family:Microsoft Yahei;\
    /*字体大小为20点*/\
    font-size:20pt;\
    /*字体颜色为白色*/\
    color:white;\
    /*背景颜色*/\
    background-color:rgb(0, 0, 0);\
    /*边框圆角半径为8像素*/\
    border-radius:8px;\
}\
/*按钮悬停态*/\
QPushButton:hover\
{\
    /*背景颜色*/\
    background-color:rgb(255, 0, 204);\
}\
/*按钮按下态*/\
QPushButton:pressed\
{\
    /*背景颜色*/\
    background-color:rgb(255, 239, 213);\
    /*左内边距为3像素,让按下时字向右移动3像素*/\
    padding-left:3px;\
    /*上内边距为3像素,让按下时字向下移动3像素*/\
    padding-top:3px;\
}"))

#define  QPushButton_STYTLE_North (QString("\
/*按钮普通态*/\
QPushButton\
{\
    font-family:Microsoft Yahei;\
    /*字体大小为20点*/\
    font-size:20pt;\
    /*字体颜色为白色*/\
    color:black;\
    /*背景颜色*/\
    background-color:rgb(255, 255, 255);\
    /*边框圆角半径为8像素*/\
    border-radius:8px;\
}\
/*按钮悬停态*/\
QPushButton:hover\
{\
    /*背景颜色*/\
    background-color:rgb(0, 255, 255);\
}\
/*按钮按下态*/\
QPushButton:pressed\
{\
    /*背景颜色*/\
    background-color:rgb(255, 239, 213);\
    /*左内边距为3像素,让按下时字向右移动3像素*/\
    padding-left:3px;\
    /*上内边距为3像素,让按下时字向下移动3像素*/\
    padding-top:3px;\
}"))

#define  QPushButton_STYTLE_South (QString("\
/*按钮普通态*/\
QPushButton\
{\
    font-family:Microsoft Yahei;\
    /*字体大小为20点*/\
    font-size:20pt;\
    /*字体颜色为白色*/\
    color:black;\
    /*背景颜色*/\
    background-color:rgb(255, 0, 0);\
    /*边框圆角半径为8像素*/\
    border-radius:8px;\
}\
/*按钮悬停态*/\
QPushButton:hover\
{\
    /*背景颜色*/\
    background-color:rgb(0, 255, 0);\
}\
/*按钮按下态*/\
QPushButton:pressed\
{\
    /*背景颜色*/\
    background-color:rgb(255, 239, 213);\
    /*左内边距为3像素,让按下时字向右移动3像素*/\
    padding-left:3px;\
    /*上内边距为3像素,让按下时字向下移动3像素*/\
    padding-top:3px;\
}"))

class Dialog : public QDialog
{
    Q_OBJECT

public:
    Dialog(QWidget *parent = 0);
    ~Dialog();
private:
    QPushButton* btnEast;
    QPushButton* btnWest ;
    QPushButton* btnNorth;
    QPushButton* btnSouth;
    QPushButton* btnMiddle;
private slots:
    void mySlotEast();
    void mySlotWest();
    void mySlotNorth();
    void mySlotSouth();
    void mySlotMiddle();
};

#endif // DIALOG_H

二、dialog.cpp

#include "dialog.h"

Dialog::Dialog(QWidget *parent)
    : QDialog(parent)
{
    resize(500,500);
    move(710,290);
    setStyleSheet("background-color:rgb(255, 228, 196)");

    btnEast=new QPushButton("东",this);
    btnEast->resize(150,100);
    btnEast->move(325,200);
    btnEast->setStyleSheet(QPushButton_STYTLE_East);
    connect(btnEast,SIGNAL(clicked()),this,SLOT(mySlotEast()));

    btnWest=new QPushButton("西",this);
    btnWest->resize(150,100);
    btnWest->move(25,200);
    btnWest->setStyleSheet(QPushButton_STYTLE_West);
    connect(btnWest,SIGNAL(clicked()),this,SLOT(mySlotWest()));

    btnSouth=new QPushButton("南",this);
    btnSouth->resize(150,100);
    btnSouth->move(175,325);
    btnSouth->setStyleSheet(QPushButton_STYTLE_South);
    connect(btnSouth,SIGNAL(clicked()),this,SLOT(mySlotSouth()));

    btnNorth=new QPushButton("北",this);
    btnNorth->resize(150,100);
    btnNorth->move(175,75);
    btnNorth->setStyleSheet(QPushButton_STYTLE_North);
    connect(btnNorth,SIGNAL(clicked()),this,SLOT(mySlotNorth()));

    btnMiddle=new QPushButton("中",this);
    btnMiddle->resize(150,150);
    btnMiddle->move(175,175);
    btnMiddle->setStyleSheet(QPushButton_STYTLE_Middle);
    connect(btnMiddle,SIGNAL(clicked()),this,SLOT(mySlotMiddle()));

}

void Dialog::mySlotEast(){
    int x=this->x();
    int y=this->y();
    x+=10;
    qDebug()<<"东移至"<<x<<y;
    move(x,y);
}
void Dialog::mySlotWest(){
    int x=this->x();
    int y=this->y();
    x-=10;
    qDebug()<<"西移至"<<x<<y;
    move(x,y);
}
void Dialog::mySlotNorth(){
    int x=this->x();
    int y=this->y();
    y-=10;
    qDebug()<<"北移至"<<x<<y;
    move(x,y);
}
void Dialog::mySlotSouth(){
    int x=this->x();
    int y=this->y();
    y+=10;
    qDebug()<<"南移至"<<x<<y;
    move(x,y);
}
void Dialog::mySlotMiddle(){
    int x=710;
    int y=290;
    qDebug()<<"恢复中央位置"<<x<<y;
    move(x,y);
}
Dialog::~Dialog()
{

}

三、main.cpp

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

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Dialog w;
    w.show();

    return a.exec();
}

三、运行演示

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

蜂蜂

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值