QT控制元件的簡單使用(持續更新)
阿新 • • 發佈:2019-01-13
記錄一下學習QT中,用一些小例子來簡單描述一下控制元件的使用方法:
1、QPixmap的使用
MainWindow.h
#ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #include <QPixmap> #include <QLabel> 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; }
執行結果:
2、QTimer的使用
MainWindow.h
#ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #include <QTimer> #include <QLabel> #include <QPixmap> 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 <QMainWindow>
#include <QTimer>
#include <QLabel>
#include <QTime>
#include <QString>
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;
}
執行結果: