天天看點

《 QT5.9 c++ 開發指南》3~4章簡單操作                                    QT程式設計記錄補充

                                    QT程式設計記錄補充

第一章,第二章傳送門

第三章 :QT類庫概述

          本章講述了一些知識點,都是比較基礎的概念,對應的知識點請檢視對應書籍,慢慢了解,這裡補充不在細講。

第四章 :常用界面設計元件

4.1  字元串與輸入輸出

         4.1.1 字元串與數值之間的轉換

         先上布局,本例子為進制轉換程式:

《 QT5.9 c++ 開發指南》3~4章簡單操作                                    QT程式設計記錄補充

界面設計時使用最多的元件恐怕就是 QLable 與 QLineEdit,本例子就是使用:

對應頭檔案:

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>

namespace Ui {
class Widget;
}

class Widget : public QWidget
{
    Q_OBJECT

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

private slots:
    void on_btnCal_clicked();  //計算 按鍵單擊響應

    void on_btnDec_clicked();   //十進制轉換為其他進制

    void on_btnBin_clicked();   //二進制轉換為其他進制

    void on_btnHex_clicked();   //十六進制轉換為其他進制

private:
    Ui::Widget *ui;
};

#endif // WIDGET_H
           

對應的cpp:

#include "widget.h"
#include "ui_widget.h"
#include    <QString>

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

Widget::~Widget()
{
    delete ui;
}

void Widget::on_btnCal_clicked()
{ //計算 按鍵單擊響應
    int num=ui->editNum->text().toInt(); //讀取字元串為整數
    float price=ui->editPrice->text().toFloat();//讀取字元串為浮點數

    float total=num*price;//相乘計算
    QString str;
    str=str.sprintf("%.2f",total); //格式化輸出浮點數
    ui->editTotal->setText(str);//在文本框裡顯示



}

void Widget::on_btnDec_clicked()
{ //讀取十進制數,轉換為其他進制
    int val=ui->editDec->text().toInt();//讀取十進制數
    QString str=QString::number(val,16);// 顯示為16進制 的字元串
    str=str.toUpper(); //轉換為全大寫字母
    ui->editHex->setText(str);//顯示16進制字元串

    str=QString::number(val,2);// 顯示2進制的字元串
    ui->editBin->setText(str);//顯示二進制字元串
}

void Widget::on_btnBin_clicked()
{ //讀取二進制數,轉換為其他進制的數
    bool ok;

    int val=ui->editBin->text().toInt(&ok,2);//以二進制數讀入


    QString str=QString::number(val,10);//數字顯示為10進制字元串
    ui->editDec->setText(str);//顯示10進制數字元串


    str=QString::number(val,16);//顯示為十六進制字元串
    str=str.toUpper(); //全大寫字母
    ui->editHex->setText(str);//顯示十六進制字元串
}

void Widget::on_btnHex_clicked()
{//讀取16進制數,轉換為其他進制的數
    bool ok;

    int val=ui->editHex->text().toInt(&ok,16);//以十六進制數讀入

    QString str=QString::number(val,10);// 顯示為10進制字元串
    ui->editDec->setText(str);//顯示為10進制字元串

    str=QString::number(val,2);// 顯示二進制字元串
    ui->editBin->setText(str);//顯示二進制字元串
}

           

連結:本例子連結     提取碼:3d0b

 4.1.2 QString 的常用功能

       運作的結果圖:

《 QT5.9 c++ 開發指南》3~4章簡單操作                                    QT程式設計記錄補充

上面的每一個按鈕對應着QString對應的函數功能:

這裡就直接上代碼了,注釋裡面有很多介紹了:

對應頭檔案:

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>

namespace Ui {
class Widget;
}

class Widget : public QWidget
{
    Q_OBJECT

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

private slots:
    void on_pushButton_clicked();

    void on_pushButton_2_clicked();

    void on_pushButton_3_clicked();

    void on_pushButton_4_clicked();

    void on_pushButton_5_clicked();

    void on_pushButton_6_clicked();

    void on_pushButton_7_clicked();

    void on_pushButton_8_clicked();

    void on_pushButton_9_clicked();

    void on_pushButton_10_clicked();

    void on_pushButton_11_clicked();

    void on_pushButton_12_clicked();

    void on_pushButton_13_clicked();

    void on_pushButton_14_clicked();

    void on_pushButton_15_clicked();

    void on_pushButton_16_clicked();

    void on_pushButton_17_clicked();

    void on_pushButton_18_clicked();

private:
    Ui::Widget *ui;
};

#endif // WIDGET_H
           

對應cpp檔案:

#include "widget.h"
#include "ui_widget.h"

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

Widget::~Widget()
{
    delete ui;
}

void Widget::on_pushButton_clicked()
{//append()函數
    QString str1,str2;
    str1=ui->comboBox1->currentText();
    str2=ui->comboBox2->currentText();
    str1.append(str2);

    ui->edtResult->setText(str1);
}

void Widget::on_pushButton_2_clicked()
{//prepend()函數
    QString str1,str2;
    str1=ui->comboBox1->currentText();
    str2=ui->comboBox2->currentText();
    str1.prepend(str2);

    ui->edtResult->setText(str1);
}

void Widget::on_pushButton_3_clicked()
{//contains()函數
    QString str1,str2;
    str1=ui->comboBox1->currentText();
    str2=ui->comboBox2->currentText();

    bool chk;
    chk=str1.contains(str2);
    ui->checkBox->setChecked(chk);
    ui->checkBox->setText("contains()");
    ui->checkBox->sizeHint();
}

void Widget::on_pushButton_4_clicked()
{//count()函數
    QString str1=ui->comboBox1->currentText();
    int i=str1.count();
//    int i=str1.length();
    ui->spinBox->setValue(i);
    ui->LabSpin->setText("count()");
}

void Widget::on_pushButton_5_clicked()
{//size()函數
    QString str1;
    str1=ui->comboBox1->currentText();
    int i=str1.size();
    ui->spinBox->setValue(i);
    ui->LabSpin->setText("size()");

}

void Widget::on_pushButton_6_clicked()
{//endsWith()函數
    QString str1,str2;
    str1=ui->comboBox1->currentText();
    str2=ui->comboBox2->currentText();

    bool chk;
    chk=str1.endsWith(str2);
    ui->checkBox->setChecked(chk);
    ui->checkBox->setText("endsWith()");
    ui->checkBox->sizeHint();
}

void Widget::on_pushButton_7_clicked()
{//indexOf()函數
    QString str1,str2;
    str1=ui->comboBox1->currentText();
    str2=ui->comboBox2->currentText();

    int i;
    i=str1.indexOf(str2);
    ui->spinBox->setValue(i);
    ui->LabSpin->setText("indexOf()");
}

void Widget::on_pushButton_8_clicked()
{//isEmpty()函數
    QString str1;
    str1=ui->comboBox1->currentText();
    bool chk;
    chk=str1.isEmpty();
    ui->checkBox->setChecked(chk);
    ui->checkBox->setText("isEmpty()");
    ui->checkBox->sizeHint();
}

void Widget::on_pushButton_9_clicked()
{//lastIndexOf()函數
    QString str1,str2;
    str1=ui->comboBox1->currentText();
    str2=ui->comboBox2->currentText();

    int i;
    i=str1.lastIndexOf(str2);
    ui->spinBox->setValue(i);
    ui->LabSpin->setText("lastIndexOf()");
}

void Widget::on_pushButton_10_clicked()
{//startsWith()函數
    QString str1,str2;
    str1=ui->comboBox1->currentText();
    str2=ui->comboBox2->currentText();

    bool chk;
    chk=str1.startsWith(str2);
    ui->checkBox->setChecked(chk);
    ui->checkBox->setText("startsWith()");
    ui->checkBox->sizeHint();
}

void Widget::on_pushButton_11_clicked()
{//toUpper()函數
    QString str1,str2;
    str1=ui->comboBox1->currentText();
    str2=str1.toUpper();

    ui->edtResult->setText(str2);
}

void Widget::on_pushButton_12_clicked()
{//toLower()函數
    QString str1,str2;
    str1=ui->comboBox1->currentText();
    str2=str1.toLower();

    ui->edtResult->setText(str2);
}

void Widget::on_pushButton_13_clicked()
{//trimmed()函數
    QString str1;
    str1=ui->comboBox1->currentText();
    str1=str1.trimmed();

    ui->edtResult->setText(str1);

}

void Widget::on_pushButton_14_clicked()
{//section()函數
    int i;
    QString str1,str2,str3;
    str1=ui->comboBox1->currentText();
    i=ui->spinBox->value();
//    str2=str1.section('\\',2,2);
    str3=ui->comboBox2->currentText();
    if (QString::compare(str3,"\\",Qt::CaseInsensitive)==0)
        str2=str1.section('\\',i,i+1); //
    else
        str2=str1.section(str3,i,i+1); //

    ui->edtResult->setText(str2);
}

void Widget::on_pushButton_15_clicked()
{//left()函數
    QString str1,str2;
    str1=ui->comboBox1->currentText();
    int v=ui->spinBox->value();
    str2=str1.left(v);
    ui->edtResult->setText(str2);
}

void Widget::on_pushButton_16_clicked()
{//right()函數
    QString str1,str2;
    str1=ui->comboBox1->currentText();
    int cnt=str1.size();
    int v=ui->spinBox->value();
    str2=str1.right(cnt-v-1);
    ui->edtResult->setText(str2);
}

void Widget::on_pushButton_17_clicked()
{//simplified()函數
    QString str1;
    str1=ui->comboBox1->currentText();
    str1=str1.simplified();
    ui->edtResult->setText(str1);
}

void Widget::on_pushButton_18_clicked()
{//isNull()函數
    QString str1;
    str1=ui->comboBox1->currentText();
    bool chk;
    chk=str1.isNull();
    ui->checkBox->setChecked(chk);
    ui->checkBox->setText("isNull()");
    ui->checkBox->sizeHint();
}
           

append () 在字元串的後面添加字元串 , prepend () 在字元串的前面添加字元串

功能很多這裡就不多說了,可以查對應函數功能,或者書籍。

連結:4.2QStirng例子    提取碼:y0jr 

4.2 SpinBox 的使用

本例子與第一個例子相同,隻是采用了不同的空間,不需要把QString在轉化成對應的資料。

       先上圖:

《 QT5.9 c++ 開發指南》3~4章簡單操作                                    QT程式設計記錄補充

    使用 QSpinBox 和 QdoubleSpinBox 進行數值量的輸入輸出很友善,

《 QT5.9 c++ 開發指南》3~4章簡單操作                                    QT程式設計記錄補充

對應的屬性要改,話不多說上例子吧:

對應的頭檔案;

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>

namespace Ui {
class Widget;
}

class Widget : public QWidget
{
    Q_OBJECT

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

private slots:
    void on_btnCal_clicked(); //計算按鍵 clicked()響應

    void on_btnDec_clicked();//讀十進制數再用其他進制顯示 的按鍵的 clicked()響應

    void on_btnBin_clicked();//讀二進制數再用其他進制顯示 的按鍵的 clicked()響應

    void on_btnHex_clicked();//讀十六進制數再用其他進制顯示 的按鍵的 clicked()響應

private:
    Ui::Widget *ui;
};

#endif // WIDGET_H
           

對應的cpp檔案:

#include "widget.h"
#include "ui_widget.h"
#include    <QString>

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
//“數量”和“單價”兩個SpinBox的valueChanged()信号與on_btnCal_clicked()槽關聯
    QObject::connect(ui->spinNum,SIGNAL(valueChanged(int)),this,SLOT(on_btnCal_clicked()));  //
    QObject::connect(ui->spinPrice,SIGNAL(valueChanged(double)),this,SLOT(on_btnCal_clicked()));

    QObject::connect(ui->spinDec,SIGNAL(valueChanged(int)),
                     this,SLOT(on_btnDec_clicked()));//spinDec 十進制顯示的SpinBox

    QObject::connect(ui->spinBin,SIGNAL(valueChanged(int)),
                     this,SLOT(on_btnBin_clicked()));//spinBin 二進制顯示的SpinBox

    QObject::connect(ui->spinHex,SIGNAL(valueChanged(int)),
                     this,SLOT(on_btnHex_clicked()));//spinHex 十六進制顯示的SpinBox
}

Widget::~Widget()
{
    delete ui;
}

void Widget::on_btnCal_clicked()
{ //計算按鍵
    int num=ui->spinNum->value(); //讀取數量,直接是整數
    float price=ui->spinPrice->value();//讀取單價,直接是浮點數
    float total=num*price;//相乘計算
    ui->spinTotal->setValue(total); //直接顯示浮點數
}


void Widget::on_btnDec_clicked()
{ //讀取十進制,以其他進制顯示
    int val;
    val=ui->spinDec->value();//讀取十進制數
    ui->spinBin->setValue(val); //設定數值即可,自動以二進制顯示
    ui->spinHex->setValue(val); //設定數值即可,自動以十六進制顯示
}

void Widget::on_btnBin_clicked()
{ //讀取二進制,以其他進制顯示
    int val=ui->spinBin->value();//讀取spinBin裡的二進制數,得到整數
    ui->spinDec->setValue(val);//設定數值即可,自動以十進制顯示
    ui->spinHex->setValue(val);//設定數值即可,自動以十六進制顯示
}

void Widget::on_btnHex_clicked()
{ //讀取十六進制,以其他進制顯示
    int val;
    val=ui->spinHex->value();//讀取 spinHex 裡的十六進制數,得到整數
    ui->spinDec->setValue(val);//設定數值即可,自動以十進制顯示
    ui->spinBin->setValue(val);//設定數值即可,自動以二進制顯示
}

           

連結:4.3Spin例子  提取碼:a3qh

今天先補充到這裡,剩下的明天更新,喜歡的就點個關注,需要資源的請私信我。                

繼續閱讀