QT控件的简单使用(持续更新)

记录一下学习QT中,用一些小例子来简单描述一下控件的使用方法:

1、QPixmap的使用

MainWindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include 
#include 
#include 

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT
    
public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();
    
private:
    Ui::MainWindow *ui;
    QPixmap *Picture ;
    QLabel  *lable ;
};

#endif // MAINWINDOW_H

MainWindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    lable  = new QLabel(this);
    this->setWindowFlags(Qt::FramelessWindowHint | Qt::WindowTitleHint | Qt::WindowSystemMenuHint);
    //设置图片资源到QPixmap上
    Picture = new QPixmap("C:/Users/Administrator/Desktop/QT_simple/QPixmap/QPixmap_Test/linux_logo.png");
    this->setFixedSize(Picture->height(),Picture->width());
    lable->setGeometry(Picture->height(),Picture->width(),Picture->height(),Picture->width());
    lable->move(0,0);
    //将图片设置到QLabel控件上
    lable->setPixmap(*Picture);
    lable->show();
}

MainWindow::~MainWindow()
{
    delete Picture ;
    delete lable ;
    delete ui;
}

运行结果:

QT控件的简单使用(持续更新)_第1张图片

2、QTimer的使用

MainWindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include 
#include 
#include 
#include 

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT
    
public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();
    
private:
    Ui::MainWindow *ui;
    QTimer *timer ;
    QLabel *lable ;
    QPixmap *Picture ;

private slots:
    void Change_Picture();
};

#endif // MAINWINDOW_H

MainWindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    lable  = new QLabel(this);
    this->setWindowFlags(Qt::FramelessWindowHint | Qt::WindowTitleHint | Qt::WindowSystemMenuHint);
    Picture = new QPixmap("C:/Users/Administrator/Desktop/QT_simple/QPixmap/QPixmap_Test/up.png");
    this->setFixedSize(Picture->height(),Picture->width());
    lable->setGeometry(Picture->height(),Picture->width(),Picture->height(),Picture->width());
    lable->move(0,0);
    lable->setPixmap(*Picture);
    lable->show();
    timer = new QTimer(this);
    connect(timer,SIGNAL(timeout()),this,SLOT(Change_Picture()));
    //开启一个定时器,用于图片的切换,每100ms切换一次
    timer->start(100);
}
//定时器槽函数,用于更换图片
void MainWindow::Change_Picture()
{
    static int status = 0 ;
    switch(status)
    {
        case 0:
            this->lable->setPixmap(QPixmap("C:/Users/Administrator/Desktop/QT_simple/QPixmap/QPixmap_Test/up.png"));
            status++ ;
            break ;
        case 1:
            this->lable->setPixmap(QPixmap("C:/Users/Administrator/Desktop/QT_simple/QPixmap/QPixmap_Test/down.png"));
            status++ ;
            break ;
        case 2:
            this->lable->setPixmap(QPixmap("C:/Users/Administrator/Desktop/QT_simple/QPixmap/QPixmap_Test/left.png"));
            status++ ;
            break ;
        case 3:
            this->lable->setPixmap(QPixmap("C:/Users/Administrator/Desktop/QT_simple/QPixmap/QPixmap_Test/right.png"));
            status = 0 ;
            break ;
    }

}

MainWindow::~MainWindow()
{
    delete timer ;
    delete Picture ;
    delete lable ;
    delete ui;
}

运行结果:

以下图片以up、dow、left、right的顺序,然后以100ms的执行速度切换。

3、QTime的使用

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include 
#include 
#include 
#include 
#include 

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT
    
public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();
    
private:
    Ui::MainWindow *ui;
    QTime  *time ;
    QTimer *timer ;
    QLabel *lable ;
    QString *time_Stirng ;

private slots:
    void Change_Time();
};

#endif // MAINWINDOW_H

Mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    this->setWindowFlags(Qt::FramelessWindowHint | Qt::WindowTitleHint | Qt::WindowSystemMenuHint);
    time  = new QTime();
    timer = new QTimer(this);
    time_Stirng = new QString() ;
    lable  = new QLabel(this);
    time_Stirng->clear();
    lable->setGeometry(75,20,150,20);
    lable->move(0,0);
    connect(timer,SIGNAL(timeout()),this,SLOT(Change_Time()));
    lable->show();
    timer->start(1000);
}

void MainWindow::Change_Picture()
{
    //获取当前的时间
    *time = time->currentTime();
    //将时间转换成字符串形式,并追加到QStirng上保存
    time_Stirng->append(time->toString("hh:mm:ss"));
    //将QStirng显示到QLabel上
    lable->setText(*time_Stirng);
    time_Stirng->clear();
}

MainWindow::~MainWindow()
{
    delete time ;
    delete timer ;
    delete lable ;
    delete ui;
}

运行结果:

你可能感兴趣的:(QT软件开发)