当前位置:网站首页>Qt个人学习总结

Qt个人学习总结

2022-07-04 22:34:00 Yinzz2

Qt学习视频

学习Qt第一个案例:

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

int main(int argc, char *argv[])
{
    //创建qt应用程序对象
    QApplication a(argc, argv);
    //创建标签控件
    QLabel label("Hello Qt!");
    //显示标签控件
    label.show();
    //让应用程序进入控件

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

 Qt中文编码问题

Qt5可以正确理解uft-8格式编码,并将其自动转换为内部的unicode编码。但如果使用其它格式的外部编码,比如,windows中常用的GBK编码,将会出现乱码现象

解决方法:

//包含头文件
#include<qtextcodec.h> 

//创建编码对象
QTextCodec* coder = QTextCodec::codecForName("gbk");

//将显示gbk编码字符串转换为unicode
QLabel label(coder->toUnicode("爹"));
label.show();

第二个案例父窗口

常用父窗口有三个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);
    //创建标签控件,停靠在父窗口上面

    QLabel label("我是标签",&parent);
    label.move(20,40);

    //创建按钮控件,停靠在父窗口上面
    QPushButton button("我是按键",&parent);
    button.move(20,100);
    button.resize(100,100);

    //不需要delete
    QPushButton* button2 = new QPushButton("我是按键",&parent);
    button2->move(180,100);
    button2->resize(100,100);

    //父窗口显示,上面的控件一样显示
    parent.show();
    return app.exec();

}

第三个案例信号与槽

    //点击按钮关闭标签
    QObject::connect(&button,SIGNAL(clicked(void)),&label,SLOT(close(void)));
    
    //增加退出按钮,实现按住按钮退出应用程序
    QObject::connect(&button2,SIGNAL(pressed(void)),&app,SLOT(quit(void)));
    //SLOT(closeAllWindows(void))
    //&parent,SLOT(close(void))

通过信号与槽,实现滑块(QSlider)与选值框(QSpinBox)同步运行

配置滑块与选值框时候,要学会利用Qt Assistant

int main(int agrc,char** agrv)
{
    QApplication app(agrc,agrv);
    //设置一个父窗口
    QDialog parent;
    parent.resize(300,125);
    //设置一个滑块,范围1-100
    QSlider slider(Qt::Horizontal,&parent);
    slider.move(20,75);
    slider.setRange(1,100);

    //设置一个托条,范围1-100
    QSpinBox spin(&parent);
    spin.setRange(1,100);
    spin.move(120,75);

    //相互建立联系
    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();
}

第四个案例时间获取器

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(配置参数多查Qt assistant)

#include<test.h>
#include<QFont>
timeDialog::timeDialog(void)
{
    setWindowTitle("时间获取器");
    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("获取时间",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();
}

第五个案例加法计算器(使用Qt designed)

.ui

 主要学习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);
}

上面在构造函数上架冒号用法是对类成员进行初始化,析构函数一般用于善后。

第六个案例Qt设计师

.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("登录成功");
        close();
    }
    else
    {
    QMessageBox msgBox(QMessageBox::Critical,
                       "Error",
                       "输入错误",
                       QMessageBox::Ok,
                       this);
    msgBox.exec();
    }

}

void testDialog::cancel_solve(void)
{
    QMessageBox MsgBox(QMessageBox::Question,
                       "登录",
                       "确定取消登录?",
                       QMessageBox::Ok|QMessageBox::No,
                       this);
    if(MsgBox.exec()==QMessageBox::Ok)
    {
        close();
    }

}

main.cpp省略,最终效果如下:

原网站

版权声明
本文为[Yinzz2]所创,转载请带上原文链接,感谢
https://blog.csdn.net/weixin_51062259/article/details/125463997