Qt界面設計基礎

1、安裝Qt相關基本組件:html

 在ubuntu上安裝,能夠直接使用以下的命令來安裝:git

sudo apt-get install ubuntu-sdk

詳細的安裝方法能夠參考這篇文章:https://blog.csdn.net/thomasqiujs/article/details/44154845github

Qt Creator的初級入門視頻能夠參考這裏的免費教程:算法

一、  http://space.bilibili.com/84360636/#/index數據庫

二、 https://www.zhihu.com/question/22410725編程

2、Qt工程結構基本介紹:ubuntu

  首先介紹一下Qt Project的工程結構,以下圖所示,工程中主要包含的文件有:MusicPlayer.pro  mainwindow.h  main. cpp   mainwindow.cpp   mainwindow.ui 網絡

下面介紹各個部分代碼的主要做用:app

a) Musicplayer.pro文件socket

主要內容以下所示,文件的功能是加入到工程中包括的庫的內容,相似於Makefile的功能,QT+=core gui multimedia表示加入了這些須要使用的模塊,定義了目標文件TARGET,定義了源文件和頭文件等等。

#-------------------------------------------------
#
# Project created by QtCreator 2018-04-04T09:57:21
#
#-------------------------------------------------
QT       += core gui multimedia
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = MusicPlayer
TEMPLATE = app
SOURCES += main.cpp\
        mainwindow.cpp
HEADERS  += mainwindow.h
FORMS    += mainwindow.ui
View Code

b) mainwindow.h文件

主要內容以下,包括了須要使用到的頭文件,如定時器QTimer、媒體相關QMediaPlayer等等,頭文件還須要定義的是private slots,私有槽(信號和槽相對應)下面寫的是槽對應須要執行的函數,最後是private,私有變量對象,對應相關組件的調用。

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QMediaPlayer>
#include <QMediaPlaylist>
#include <QMultimedia>
#include <QMediaMetaData>
#include <QTimer>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

private slots:

    void on_NextSong_clicked(bool checked);

    void on_PrevSong_clicked(bool checked);

    void on_Volume_valueChanged(int value);

    void on_SongChoose_sliderMoved(int position);

    void on_openlocal_media();

    void on_Play_Puase_clicked(bool checked);

    void on_playProgressUpdate();

private:
    Ui::MainWindow *ui;
    QMediaPlayer *mediaPlayer;
    QMediaPlaylist *localMediaPlaylist;
    QTimer *progressTimer;
};

#endif // MAINWINDOW_H
View Code

c) main.cpp文件

 文件主要用來執行對應的程序功能,讓系統運行起來

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

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

    return a.exec();
}
View Code

d) mainwindow.cpp文件

 文件的主要功能包括了鏈接信號與槽的對應關係初始化mainwindow.h文件中的變量對象實現對應的槽函數的功能

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

#include <QFileDialog>

#include <QDataStream>

#include <QFile>

#include <QDebug>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);



    this->mediaPlayer = new QMediaPlayer(this);
    this->localMediaPlaylist = new QMediaPlaylist(this);
    this->mediaPlayer->setPlaylist(this->localMediaPlaylist);
    this->mediaPlayer->setVolume(50); //Set default Volume Value

    this->progressTimer = new QTimer(this);
    this->progressTimer->setInterval(100); //100ms
    this->progressTimer->start();

    connect(this->ui->NextSong,SIGNAL(clicked(bool)),this,SLOT(on_NextSong_clicked())); //Single connect to SLOT
    connect(this->ui->PrevSong,SIGNAL(clicked(bool)),this,SLOT(on_PrevSong_clicked()));

    connect(this->ui->Volume,SIGNAL(valueChanged(int)),this,SLOT(on_Volume_valueChanged()));
    connect(this->ui->SongChoose,SIGNAL(sliderMoved(int)),this,SLOT(on_SongChoose_sliderMoved()));

    connect(this->ui->actionOpenLocalMedia,SIGNAL(triggered(bool)),this,SLOT(on_openlocal_media()));

    connect(this->ui->Play_Puase,SIGNAL(clicked(bool)),this,SLOT(on_Play_Puase_clicked()));

    connect(this->progressTimer,SIGNAL(timeout()),this,SLOT(on_playProgressUpdate()));
}

MainWindow::~MainWindow()
{
    delete ui;
    delete this->mediaPlayer;
    delete this->localMediaPlaylist;
}


void MainWindow::on_NextSong_clicked(bool checked)
{
    qDebug() << "on_NextSong_clicked is pushed";
    this->mediaPlayer->playlist()->next();
}

void MainWindow::on_PrevSong_clicked(bool checked)
{
    qDebug() << "on_PrevSong_clicked is pushed";
    this->mediaPlayer->playlist()->previous();
}

void MainWindow::on_Volume_valueChanged(int value)
{
    qDebug()<< value;
    this->mediaPlayer->setVolume(value);
}

void MainWindow::on_SongChoose_sliderMoved(int position)
{
    qDebug()<< position;
    float percent = (position*1.0)/this->ui->SongChoose->maximum();
    long value = this->mediaPlayer->duration()*percent;
    this->mediaPlayer->setPosition(value);
}

void MainWindow::on_openlocal_media()
{
    QStringList fileNamelist;
    fileNamelist = QFileDialog::getOpenFileNames(this,tr("select local files"),"~/",tr("MP3/MP4 Files(*.mp3 *.mp4);;")); //Read file with Regex Rules.
    if(!fileNamelist.isEmpty())
    {
        qDebug() << fileNamelist;
        this->localMediaPlaylist->clear(); //Clear the PlayList
        foreach (const QString &fileName,fileNamelist) {
            QMediaContent media = QMediaContent(QUrl::fromLocalFile(fileName)); //Add the media into the PlayList
            this->localMediaPlaylist->addMedia(media);
        }
        this->localMediaPlaylist->setCurrentIndex(0); //Set the Current media when program begining
    }else{

    }
    return ;
}

void MainWindow::on_Play_Puase_clicked(bool checked)
{
    qDebug() << "Play or Pause?";
    if(this->mediaPlayer->state() == QMediaPlayer::PlayingState)
    {
        this->mediaPlayer->pause();
    }else
    {
        this->mediaPlayer->setVolume(this->ui->Volume->value()); //Choose current volume to be the current media!
        this->mediaPlayer->play();
    }
}

void MainWindow::on_playProgressUpdate()
{
    long pos = this->mediaPlayer->position();
    long duration = this->mediaPlayer->duration();

    int value = (1.0*pos/duration)*100;

    this->ui->SongChoose->setValue(value);
}
View Code

e) mainwindow.ui 文件

 文件的功能是使用HTML1.0對GUI界面的描述,描述各個button,slider,Dial,Text等等組件的位置信息,大小信息等等,以下圖所示:

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>MainWindow</class>
 <widget class="QMainWindow" name="MainWindow">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>400</width>
    <height>300</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>MainWindow</string>
  </property>
  <widget class="QWidget" name="centralWidget">
   <widget class="QPushButton" name="Play_Puase">
    <property name="geometry">
     <rect>
      <x>120</x>
      <y>160</y>
      <width>31</width>
      <height>31</height>
     </rect>
    </property>
    <property name="text">
     <string>Play</string>
    </property>
   </widget>
   <widget class="QPushButton" name="NextSong">
    <property name="geometry">
     <rect>
      <x>10</x>
      <y>150</y>
      <width>81</width>
      <height>27</height>
     </rect>
    </property>
    <property name="text">
     <string>Next Song</string>
    </property>
   </widget>
   <widget class="QLabel" name="label">
    <property name="geometry">
     <rect>
      <x>0</x>
      <y>0</y>
      <width>321</width>
      <height>51</height>
     </rect>
    </property>
    <property name="font">
     <font>
      <pointsize>28</pointsize>
      <italic>true</italic>
      <underline>false</underline>
      <strikeout>false</strikeout>
     </font>
    </property>
    <property name="cursor">
     <cursorShape>BlankCursor</cursorShape>
    </property>
    <property name="text">
     <string>Qt interface Demo!</string>
    </property>
    <property name="alignment">
     <set>Qt::AlignCenter</set>
    </property>
   </widget>
   <widget class="QDial" name="Volume">
    <property name="geometry">
     <rect>
      <x>180</x>
      <y>150</y>
      <width>50</width>
      <height>64</height>
     </rect>
    </property>
    <property name="value">
     <number>50</number>
    </property>
   </widget>
   <widget class="QSlider" name="SongChoose">
    <property name="geometry">
     <rect>
      <x>10</x>
      <y>210</y>
      <width>231</width>
      <height>29</height>
     </rect>
    </property>
    <property name="orientation">
     <enum>Qt::Horizontal</enum>
    </property>
   </widget>
   <widget class="QPushButton" name="PrevSong">
    <property name="geometry">
     <rect>
      <x>10</x>
      <y>180</y>
      <width>81</width>
      <height>27</height>
     </rect>
    </property>
    <property name="text">
     <string>Prev Song</string>
    </property>
   </widget>
   <widget class="QLabel" name="label_2">
    <property name="geometry">
     <rect>
      <x>10</x>
      <y>70</y>
      <width>271</width>
      <height>51</height>
     </rect>
    </property>
    <property name="font">
     <font>
      <pointsize>22</pointsize>
      <italic>true</italic>
      <underline>false</underline>
      <strikeout>false</strikeout>
     </font>
    </property>
    <property name="cursor">
     <cursorShape>BlankCursor</cursorShape>
    </property>
    <property name="text">
     <string>Music Player</string>
    </property>
    <property name="alignment">
     <set>Qt::AlignCenter</set>
    </property>
   </widget>
   <widget class="QLabel" name="label_3">
    <property name="geometry">
     <rect>
      <x>220</x>
      <y>80</y>
      <width>191</width>
      <height>51</height>
     </rect>
    </property>
    <property name="font">
     <font>
      <pointsize>10</pointsize>
      <italic>true</italic>
      <underline>false</underline>
      <strikeout>false</strikeout>
     </font>
    </property>
    <property name="cursor">
     <cursorShape>BlankCursor</cursorShape>
    </property>
    <property name="text">
     <string>Designed by : mm1994uestc</string>
    </property>
    <property name="alignment">
     <set>Qt::AlignCenter</set>
    </property>
   </widget>
  </widget>
  <widget class="QMenuBar" name="menuBar">
   <property name="geometry">
    <rect>
     <x>0</x>
     <y>0</y>
     <width>400</width>
     <height>25</height>
    </rect>
   </property>
   <widget class="QMenu" name="menuFile">
    <property name="title">
     <string>File</string>
    </property>
    <addaction name="actionOpenLocalMedia"/>
   </widget>
   <addaction name="menuFile"/>
  </widget>
  <widget class="QToolBar" name="mainToolBar">
   <attribute name="toolBarArea">
    <enum>TopToolBarArea</enum>
   </attribute>
   <attribute name="toolBarBreak">
    <bool>false</bool>
   </attribute>
  </widget>
  <widget class="QStatusBar" name="statusBar"/>
  <action name="actionOpenLocalMedia">
   <property name="text">
    <string>OpenLocalMedia</string>
   </property>
  </action>
 </widget>
 <layoutdefault spacing="6" margin="11"/>
 <resources/>
 <connections/>
</ui>
View Code

 以上基本介紹了一個簡單工程的相關內容。

3、基於Qt的各個模塊的Demo應用程序編程:

1) 基本文件打開與保存: http://www.cnblogs.com/uestc-mm/p/8946698.html

2) 鼠標事件監控: http://www.cnblogs.com/uestc-mm/p/8946712.html

3) 網絡通信socket連接:http://www.cnblogs.com/uestc-mm/p/8954723.html

4) 串口數據傳輸: http://www.cnblogs.com/uestc-mm/p/8946722.html

5) 鍵盤數據監控: http://www.cnblogs.com/uestc-mm/p/8946731.html

6) 音視頻播放: http://www.cnblogs.com/uestc-mm/p/8946736.html

7) 數據算法中間處理:

8) 攝像頭數據獲取:

9) 簡單的數據庫接口: http://www.cnblogs.com/uestc-mm/p/8946753.html

10) 其餘-待添加學習:

11) 程序打包的相關參考:https://github.com/mm1994uestc/QTPro-Under-Ubuntu-SDK/tree/master/PackageWorkSpace

工程參考連接:https://github.com/mm1994uestc/QTPro-Under-Ubuntu-SDK

轉載請聯繫我,並務必附上出處!謝謝!

相關文章
相關標籤/搜索