1. 程式人生 > 其它 >QT學習筆記(1)

QT學習筆記(1)

QT學習筆記(1)

選單欄和工具欄

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include<QMenuBar>
#include<QMenu>
#include<QAction>
#include<QtDebug>
#include<QToolBar>
#include<QPushButton>
#include<QStatusBar>
#include<QLabel>
#include<QTextEdit>
#include<QDockWidget>


MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    //選單欄
    QMenuBar* mbar=menuBar();
    QMenu *mFile= mbar->addMenu("檔案");
    QMenu *mEdit= mbar->addMenu("編輯");
    QAction *aNew= mFile->addAction("新建");

    connect(aNew,QAction::triggered,
            [=](){qDebug()<<"點選新建按鈕"; }
    );
    mFile->addSeparator();
    QAction *aOpen= mFile->addAction("開啟");
    connect(aOpen,QAction::triggered,
            [=](){qDebug()<<"點選編輯按鈕";}
    );
    //工具欄
    QToolBar *tbar=addToolBar("tb");
    tbar->addAction(aNew);
    QPushButton *b=new QPushButton("(^-^)");

    tbar->addWidget(b);
    connect(b,QPushButton::clicked,
            [=](){b->setText("o(╥﹏╥)o");}
    );
    //狀態列
QStatusBar*sbar=statusBar();
QLabel *l=new QLabel();
l->setText("狀態列");
sbar->addWidget(l);
sbar->addWidget(new QLabel("222"));
//從右往左新增addPermanentWidget
sbar->addPermanentWidget(new QLabel("333"));
//文字編輯器
QTextEdit *t=new QTextEdit(this);
setCentralWidget(t);
//浮動視窗
QDockWidget* dock=new QDockWidget();
addDockWidget(Qt::RightDockWidgetArea,dock);
QTextEdit *t2=new QTextEdit(this);
dock->setWidget(t2);


}

執行結果

模態對話方塊和非模態對話方塊

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include<QMenu>
#include<QMenuBar>
#include<QAction>
#include<QDialog>


MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    QMenuBar *mbar=menuBar();
    setMenuBar(mbar);
    QMenu *m=mbar->addMenu("對話方塊");
    QAction *ac1=m->addAction("模態對話方塊");
    connect(ac1,QAction::triggered,
            [=](){    QDialog *dia1=new QDialog(this);
        dia1->resize(200,100);
        dia1->exec();}        );

    QAction *ac2=m->addAction("非模態對話方塊");
    connect(ac2,QAction::triggered,
            [=](){    QDialog *dia2=new QDialog(this);
 dia2->resize(200,100);
        dia2->setAttribute(Qt::WA_DeleteOnClose);
        dia2->show();}        );



}

執行結果

我的計算器

#include "cal.h"
#include "ui_cal.h"
#include<QString>

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

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

//通過按鍵獲取輸入
QString input="";
void Cal::on_pushButton1_clicked()
{
    input+="1";
    ui->label_display->setText(input);
}

void Cal::on_pushButton2_clicked()
{
    input+="2";
    ui->label_display->setText(input);
}

void Cal::on_pushButton3_clicked()
{
    input+="3";
    ui->label_display->setText(input);
}

void Cal::on_pushButton4_clicked()
{
    input+="4";
    ui->label_display->setText(input);
}

void Cal::on_pushButton5_clicked()
{
    input+="5";
    ui->label_display->setText(input);
}

void Cal::on_pushButton6_clicked()
{
    input+="6";
    ui->label_display->setText(input);
}

void Cal::on_pushButton7_clicked()
{
    input+="7";
    ui->label_display->setText(input);
}

void Cal::on_pushButton8_clicked()
{
    input+="8";
    ui->label_display->setText(input);
}

void Cal::on_pushButton9_clicked()
{
    input+="9";
    ui->label_display->setText(input);
}

void Cal::on_pushButton0_clicked()
{
    input+="0";
    ui->label_display->setText(input);
}

void Cal::on_pushButtonAdd_clicked()
{
    input+="+";
    ui->label_display->setText(input);
}

void Cal::on_pushButtonSub_clicked()
{
    input+="-";
    ui->label_display->setText(input);
}

void Cal::on_pushButtonMul_clicked()
{
    input+="*";
    ui->label_display->setText(input);
}

void Cal::on_pushButtonDiv_clicked()
{
    input+="/";
    ui->label_display->setText(input);
}

void Cal::on_pushButtonEqu_clicked()
{
    Cal:: Calculate( input);
    //input+="=";
    ui->label_display->setText(input);
}

void Cal::on_pushButtonClear_clicked()
{
    input="";
    ui->label_display->setText(input);
}

//計算
#include<QStringList>

void Cal:: Calculate( QString &s)
{
    int a,b;
    QStringList list;
    if(s.contains("+"))
    {
        list=  s.split("+");
        if(list.length()==2)
        {
            a=list[0].toInt();
            b=list[1].toInt();
            s.setNum(a+b);
        }
        else{    s="error";}
    }
    else if(s.contains("-"))
    {
        list=  s.split("-");
        if(list.length()==2)
        {
            a=list[0].toInt();
            b=list[1].toInt();
            s.setNum(a-b);
        }
        else    {        s="error";    }

    }
    else if(s.contains("*"))
    {
        list=  s.split("*");
        if(list.length()==2)
        {
            a=list[0].toInt();
            b=list[1].toInt();
            s.setNum(a*b);
        }
        else    {        s="error";    }

    }
    else if(s.contains("/"))
    {
        list=  s.split("/");
        if(list.length()==2)
        {
            a=list[0].toInt();
            b=list[1].toInt();
            if(b==0)
            {
                s="error";
                return;
            }
            s.setNum(a/b);
        }
        else    {        s="error";    }

    }

}

執行結果

表格測試

#include<QApplication>
#include<QWidget>
#include<QLabel>
#include<QTableWidget>
#include<QTableWidgetItem>
#include<QStringList>
#include<QDebug>
#include<QPushButton>

using namespace std;

class MyLabel:public QLabel
{

    //Q_OBJECT
public slots://自定義槽函式
    void resetText(QTableWidgetItem*item);

};
//槽函式的類外實現
void MyLabel::resetText(QTableWidgetItem *item)
{this->setText(item->text());}
//主函式
int main(int argc,char*argv[])
{
    QApplication a(argc,argv);
QWidget w;
w.setWindowTitle("我的表格視窗");
w.resize(1000,500);
QTableWidget t(4,3,&w);
t.resize(1000,400);
t.setFont(QFont("宋體",20));
t.setHorizontalHeaderLabels(QStringList()<<"標題"<<"網址"<<"作者");
t.setEditTriggers(QAbstractItemView::NoEditTriggers);
t.setItem(0,0,new QTableWidgetItem("QT學習筆記(1)"));
t.setItem(0,1,new QTableWidgetItem("https://www.cnblogs.com/zhangdezhang/p/16277071.html"));
t.setItem(0,2,new QTableWidgetItem("zdz"));
t.setItem(1,0,new QTableWidgetItem("C++學習筆記(2)"));
t.setItem(1,1,new QTableWidgetItem("https://www.cnblogs.com/zhangdezhang/p/16268859.html"));
t.setItem(1,2,new QTableWidgetItem("zdz"));
t.setItem(2,0,new QTableWidgetItem("C++學習筆記(1)"));
t.setItem(2,1,new QTableWidgetItem("https://www.cnblogs.com/zhangdezhang/p/16264787.html"));
t.setItem(2,2,new QTableWidgetItem("zdz"));

MyLabel label;
label.setText("請選擇一個單元格");
label.setParent(&w);
label.resize(900,150);
label.move( QPoint(0,400));
label.setAlignment(Qt::AlignCenter);
label.setFont(QFont("宋體",30));

QObject::connect(&t,QTableWidget::itemClicked,&label,MyLabel::resetText);

w.show();
   return a.exec();

}

執行結果

按鈕測試

#include<QApplication>
#include<QPushButton>

//按鈕測試

int main(int argc,char*argv[])//主函式
{
    QApplication a(argc,argv);//建立應用

    QWidget w;//建立視窗
    w.setWindowTitle("QT真是牛逼");//設定視窗標題
    w.setStyleSheet("QWidget{background-color:green;}");//窗體背景綠色
    QPushButton b("QPushButton按鈕",&w);//設定父物件
    b.move(100,100);//設定位置
    //設定字型大小,設定背景顏色
    b.setStyleSheet("QPushButton{font:20px;background-color:red;}");
    b.resize(200,200);//設定尺寸
    //設定訊號槽,點選按鈕--按鈕隱藏
    QObject::connect(&b,QPushButton::clicked,&b,QPushButton::hide);


    w.show();//顯示視窗
    return a.exec();//迴圈等待
}

執行結果

QT思維導圖

QT的訊號槽的訊息機制(事件/委託-事件處理函式);

QObject::connect(&b,QPushButton::clicked,&w,QWidget::close);