当前位置:网站首页>QT personal learning summary

QT personal learning summary

2022-07-04 23:15:00 Yinzz2

Qt Learning video

Study Qt The first case :

#include "QLabel"
#include <QApplication>
#include<QPushButton>

int main(int argc, char *argv[])
{
    // establish qt Application object 
    QApplication a(argc, argv);
    // Create a label control 
    QLabel label("Hello Qt!");
    // Show label controls 
    label.show();
    // Let the application enter the control 

    QPushButton button("wuhu");
    button.show();
   
    return a.exec();
}

 Qt Chinese coding problem

Qt5 It can be understood correctly uft-8 Format encoding , And automatically convert it into internal unicode code . But if you use external coding in other formats , such as ,windows Commonly used GBK code , There will be garbled code

resolvent :

// Include header file 
#include<qtextcodec.h> 

// Create coding objects 
QTextCodec* coder = QTextCodec::codecForName("gbk");

// Will be displayed gbk The encoded string is converted to unicode
QLabel label(coder->toUnicode(" Daddy "));
label.show();

The second case is the parent window

There are three common parent windows Qwidget,QMainWindow,Qdialog

#include<QApplication>
#include<QWidget>
#include<QLabel>
#include<QPushButton>
#include<QDialog>
#include<QMainWindow>

int main(int argc,char** argv)
{
    QApplication app(argc,argv);

    QDialog parent;
    parent.resize(320,240);
    parent.move(50,50);
    // Create a label control , Dock above the parent window 

    QLabel label(" I'm the label ",&parent);
    label.move(20,40);

    // Create button control , Dock above the parent window 
    QPushButton button(" I'm a button ",&parent);
    button.move(20,100);
    button.resize(100,100);

    // Unwanted delete
    QPushButton* button2 = new QPushButton(" I'm a button ",&parent);
    button2->move(180,100);
    button2->resize(100,100);

    // The parent window displays , The above controls display 
    parent.show();
    return app.exec();

}

The third case is signal and slot

    // Click the button to close the tab 
    QObject::connect(&button,SIGNAL(clicked(void)),&label,SLOT(close(void)));
    
    // Add exit button , Press and hold the button to exit the application 
    QObject::connect(&button2,SIGNAL(pressed(void)),&app,SLOT(quit(void)));
    //SLOT(closeAllWindows(void))
    //&parent,SLOT(close(void))

Through signal and slot , Implement slider (QSlider) And the selection box (QSpinBox) Synchronous operation

When configuring the slider and value selection box , Learn to use Qt Assistant

int main(int agrc,char** agrv)
{
    QApplication app(agrc,agrv);
    // Set a parent window 
    QDialog parent;
    parent.resize(300,125);
    // Set a slider , Range 1-100
    QSlider slider(Qt::Horizontal,&parent);
    slider.move(20,75);
    slider.setRange(1,100);

    // Set a brace , Range 1-100
    QSpinBox spin(&parent);
    spin.setRange(1,100);
    spin.move(120,75);

    // Connect with each other 
    QObject::connect(&slider,SIGNAL(valueChanged(int)),&spin,SLOT(setValue(int)));

    QObject::connect(&spin,SIGNAL(valueChanged(int)),&slider,SLOT(setValue(int)));

    parent.show();

    return app.exec();
}

The fourth case is time acquisition

test.h

#ifndef _TEST_H
#define _TEST_H

#include<QLabel>
#include<QDialog>
#include<QPushButton>
#include<QDebug>
#include<QTime>
#include<QVBoxLayout>

class timeDialog:public QDialog
{
    Q_OBJECT
public:
    timeDialog(void);
public slots:
    void get_Time(void);
private:
    QLabel* m_label;
    QPushButton* m_button;
};


#endif

test.cpp( Check the configuration parameters more Qt assistant)

#include<test.h>
#include<QFont>
timeDialog::timeDialog(void)
{
    setWindowTitle(" Time acquisition device ");
    m_label = new QLabel(this);
    m_label->setFrameStyle(QFrame::Panel | QFrame::Sunken);
    m_label->setLineWidth(2);
    m_label->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);

    QFont font;
    font.setPointSize(20);
    m_label->setFont(font);

    m_button = new QPushButton(" Acquisition time ",this);
    m_button->setFont(font);

    QVBoxLayout* layout=new QVBoxLayout(this);
    layout->addWidget(m_label);
    layout->addWidget(m_button);
    setLayout(layout);

    connect(m_button,SIGNAL(clicked(void)),this,SLOT(get_Time(void)));
}

void timeDialog::get_Time(void)
{
    qDebug("getTime");
    QTime time = QTime::currentTime();
    QString str = time.toString("hh:mm:ss");
    m_label->setText(str);
}

main.cpp

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

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    timeDialog w;
    w.show();
    return a.exec();
}

The fifth case is the addition calculator ( Use Qt designed)

.ui

  Main learning calculatordialog.h

#ifndef CALCULATORDIALOG_H
#define CALCULATORDIALOG_H

#include "ui_dialog.h"
#include "QValidator"
class calculatorDialog:public QDialog
{
      Q_OBJECT
public:
    calculatorDialog(void);
    ~calculatorDialog(void);
public slots:
    void enableButton(void);
    void calcCliked(void);
private:
    Ui::Dialog* ui;
};

#endif // CALCULATORDIALOG_H

calculatordialog.cpp

#include "calculatordialog.h"

calculatorDialog::calculatorDialog(void):ui(new Ui::Dialog)
{
    ui->setupUi(this);
    ui->edit_x->setValidator(new QDoubleValidator(this));
    ui->edit_y->setValidator(new QDoubleValidator(this));

    connect(ui->edit_x,SIGNAL(textChanged(QString)),this,SLOT(enableButton(void)));
    connect(ui->edit_y,SIGNAL(textChanged(QString)),this,SLOT(enableButton(void)));
    connect(ui->m_button,SIGNAL(clicked(void)),this,SLOT(calcCliked(void)));

}

calculatorDialog::~calculatorDialog(void)
{
    delete ui;
}

void calculatorDialog::enableButton(void)
{
       bool judge_x,judge_y;
       ui->edit_x->text().toDouble(&judge_x);
       ui->edit_y->text().toDouble(&judge_y);
       ui->m_button->setEnabled(judge_x && judge_y);
}

void calculatorDialog::calcCliked(void)
{
    double res = ui->edit_x->text().toDouble() + ui->edit_y->text().toDouble();
    QString str = QString::number(res);
    ui->edit_z->setText(str);
}

The colon usage on the constructor above is Initialize class members , Destructors are generally used to deal with aftermath .

The sixth case Qt The designer

.ui

dialog.h

#ifndef DIALOG_H
#define DIALOG_H

#include <QDialog>
#include <ui_dialog.h>
#include <QMessageBox>
#include <QDebug>


class testDialog:public QDialog
{
    Q_OBJECT
public:
    testDialog(void);
    ~testDialog(void);
public slots:
    void ok_solve(void);
    void cancel_solve(void);
private:
    Ui::Dialog* ui;


};

dialog.cpp

#include "dialog.h"


testDialog::testDialog(void):ui(new Ui::Dialog)
{
    ui->setupUi(this);
    connect(ui->buttonBox,SIGNAL(accepted(void)),this,SLOT(ok_solve(void)));
    connect(ui->buttonBox,SIGNAL(rejected(void)),this,SLOT(cancel_solve(void)));
}

testDialog::~testDialog(void)
{
    delete ui;
}

void testDialog::ok_solve(void)
{
    if(ui->lineEdit->text()=="ysj" && ui->lineEdit_2->text()=="123456")
    {
        qDebug(" Login successful ");
        close();
    }
    else
    {
    QMessageBox msgBox(QMessageBox::Critical,
                       "Error",
                       " Input error ",
                       QMessageBox::Ok,
                       this);
    msgBox.exec();
    }

}

void testDialog::cancel_solve(void)
{
    QMessageBox MsgBox(QMessageBox::Question,
                       " Sign in ",
                       " Confirm to cancel login ?",
                       QMessageBox::Ok|QMessageBox::No,
                       this);
    if(MsgBox.exec()==QMessageBox::Ok)
    {
        close();
    }

}

main.cpp Omit , The end result is as follows :

原网站

版权声明
本文为[Yinzz2]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/185/202207042234031980.html