博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
day01
阅读量:3699 次
发布时间:2019-05-21

本文共 6325 字,大约阅读时间需要 21 分钟。

传智课程学习笔记。

QT框架,

提供了很多工具,

通过继承,虚函数重写的方法,改变其中一些流程,

linux安装qt,

然后创建一个main.cpp,

qmake -project,产生一个工程文件,

qmake,产生一个Makefile文件,

然后需要在工程文件中添加QT += gui widgets //其中后面两个是两个库,

现在就可以make,

上面是手动方式创建,

然后./xxx就可以执行了,

我们还有qtcreator嘛~

直接在命令行输入qtcreator,就行了,顺便锁定到任务栏,下次使用方便,

QObject,

QCoreApplication,

QGuiApplication,

QApplication,

从上到下的继承关系,

QPushButton你也可以去追溯其源头,

先来个最空的框架,运行后什么也看不到,

#include 
int main( int argc, char *argv[]){ QApplication app( argc, argv); return app.exec();}
一直在运行,但是什么也看不到,

    connect( _button, SIGNAL( clicked()), this, SLOT( slotButtonClicked()));

你去看看它的定义最好,因为第2,4参数的定义有特殊处理在里面,

button这个控件,有很多种操作,

而不同的控件,操作又有些不同,

控件的操作,使得信号得以发送,

触发了this这个对象的槽函数,

我们知道类中有类成员,类操作,

QT在C++基础上,对类添加了slot,即槽,

在此附上代码,main.cpp没什么好说的,所以就不贴了,

button用指针,我想到的好处是,占用空间小,

然后这里多了一个public slots:及槽,

#ifndef MYWIDGET_H#define MYWIDGET_H#include 
#include
class MyWidget : public QWidget{ Q_OBJECTpublic: explicit MyWidget(QWidget *parent = 0); QPushButton* _button;signals:public slots: void slotButtonClicked();};#endif // MYWIDGET_H

这里的connect函数前面已经说了,
#include "MyWidget.h"#include 
MyWidget::MyWidget(QWidget *parent) : QWidget(parent){ _button = new QPushButton("this is button"); _button->setParent( this); _button->show(); _button->setGeometry( 30, 30, 100, 100); connect( _button, SIGNAL( clicked()), this, SLOT( slotButtonClicked()));}void MyWidget::slotButtonClicked(){ ;}

接下来介绍下各种控件,

一个技巧,

_button = new QPushButton("this is button");    _button->setParent( this);    _button->show();
可以直接写成,
new QPushButton("this is button", this);
后面this直接放进了父类里面,父类show之后,整个就显示出来了,

但是如果你要对控件进行一个写其它的设置,你还是得需要一个变量来接它,进而进行一些操作,

/*#include 
#include
#include
// 按钮#include
// 静态文本,可以理解为标签,#include
//单行输入框#include
//多行输入框#include
//多行文本,但是不能编辑内容#include
//小圆圈,一点就黑了,也就是选择了,#include
//一点就打勾了,#include
//下拉框选择,#include
//列表选择框,#include
//树形选择框,后面还会用到,#include
//.....#include
//一个条,可以通过拉动进行选择#include
//与上面对应,输入数字,条会变动,#include
// 进度条,不算控件,#include
//工业上用的那种数字的显示, */

布局的例子,

关于水平和竖直布局的,

代码自己看,不说了,

#include "mywidget.h"#include 
#include
#include
#include
MyWidget::MyWidget(QWidget *parent) : QWidget(parent){ QLineEdit* edit1 = new QLineEdit(); QLineEdit* edit2 = new QLineEdit(); QLineEdit* edit3 = new QLineEdit(); QHBoxLayout* layout1 = new QHBoxLayout(); QVBoxLayout* layout2 = new QVBoxLayout(); this->setLayout( layout2); layout2->addLayout( layout1); layout2->addLayout( layout1); layout1->addWidget( edit1); layout1->addWidget( edit2); layout1->addWidget( edit3); layout1->addStretch(); layout2->addStretch();}int main( int argc, char *argv[]){ QApplication app( argc, argv); MyWidget w; w.show(); return app.exec();}

这个是QGridLayout();

QLineEdit* edit1 = new QLineEdit();    QLineEdit* edit2 = new QLineEdit();    QLineEdit* edit3 = new QLineEdit();    QGridLayout* layout1 = new QGridLayout();    this->setLayout( layout1);    layout1->addWidget( edit1, 0, 0);    layout1->addWidget( edit2, 1, 1);    layout1->addWidget( edit3, 2, 2);    layout1->setColumnStretch( 1, 0);

一般用来做主窗口的,

QWidget,QDilog,QMainWindow,

int main( int argc, char *argv[]){    QApplication app( argc, argv);    MyDialog d;    //d.show(); 这里没有消息循环,下面这个有,现象就是这里Test直接就输出了,    d.exec();//而这里Test要把程序结束了,才看得到Test的输出,    QDebug << "Test" << std::endl;    return app.exec();}
比如,当登录的时候,账户密码正确之后才出现另一个窗口,就用exec,

show的话,一下就都出来了,

用户登录案例,贴主要的代码,

#ifndef MYDIALOG_H#define MYDIALOG_H#include 
#include
class MyDialog : public QDialog{ Q_OBJECTpublic: explicit MyDialog(QWidget *parent = 0); QLineEdit* _username; QLineEdit* _password;signals:public slots: void onOK(); void onExit();};#endif // MYDIALOG_H
#include "mydialog.h"#include 
#include
#include "mywidget.h"#include
#include
#include
#include
#include
#include
#include
MyDialog::MyDialog(QWidget *parent) : QDialog(parent){ QLabel* labelUserName = new QLabel("Username"); QLabel* labelPassword = new QLabel("Password"); QLineEdit* username = new QLineEdit; QLineEdit* password = new QLineEdit; password->setEchoMode(QLineEdit::Password); QPushButton* OK = new QPushButton("Login"); QPushButton* Exit = new QPushButton("Exit"); connect(OK, SIGNAL(clicked()), this, SLOT(onOK())); connect(Exit, SIGNAL(clicked()), this, SLOT(onExit())); _username = username; _password = password; QVBoxLayout* vBox = new QVBoxLayout; QHBoxLayout* hBox = new QHBoxLayout; QGridLayout* grid = new QGridLayout; QGridLayout* mainLayout = new QGridLayout; vBox->addLayout(grid); vBox->addLayout(hBox); mainLayout->setColumnStretch(0, 1); mainLayout->setColumnStretch(2, 1); mainLayout->setRowStretch(0, 1); mainLayout->setRowStretch(2, 1); mainLayout->addLayout(vBox, 1, 1); setLayout(mainLayout); grid->addWidget(labelUserName, 0, 0); grid->addWidget(labelPassword, 1, 0); grid->addWidget(username, 0, 1); grid->addWidget(password, 1, 1); hBox->addWidget(Exit); hBox->addWidget(OK);}void MyDialog::onOK(){ QString strUsername = _username->text(); QString strPassword = _password->text(); if(strUsername.length() == 0) { QMessageBox::warning(this, "错误", "用户名不能为空"); return; } if(strPassword.length() == 0) { QMessageBox::critical(this, "错误", "密码不能为空"); return; } if(strUsername=="aaa" && strPassword == "bbb") { accept(); } else { QMessageBox::critical(this, "错误", "用户名或者密码错误"); _username->setFocus(); _username->setSelection(0, 100); }}// no usevoid MyDialog::onExit(){ int ret = QMessageBox::question(this, "真的走了?", "不再试试?"); if(ret==QMessageBox::Yes) reject();}int main(int argc, char** argv){ QApplication app(argc, argv); MyDialog loginDlg; // d.show(); if(loginDlg.exec() == QDialog::Accepted) { qDebug() << "before app exec"; MyWidget w; w.show(); return app.exec(); } return -1;}

/*    QObject:要从QObject派生,要调用QObject::connect    Q_OBJECT, SIGNAL, SLOT    signals, slots, emit    1) 连接信号和槽,要求参数一致    2)信号可以连接很多槽,当信号发射时,所有槽函数都会被调用,但是顺序不确定    3)一个槽可以被很多信号连接    4)信号可以连接信号    5) 信号默认访问权限是protected    6)信号参数可以比槽多,但是相对应部分必须一致,不建议设计成这样    7)信号函数和槽函数都可以重载    8) 信号函数和槽函数都可以有默认参数    9) 在QT中,学习一个类,首先要知道这个类的功能,了解它的信号和槽函数,了解公共函数和属性*/
信号与槽的案例,回去看吧,

转载地址:http://zmbcn.baihongyu.com/

你可能感兴趣的文章
Java 8 Stream 优雅的流式编程, 过滤集合类型的数据lambda表达式
查看>>
浅谈重不重写equals和hashcode对于HashMap添加元素的影响
查看>>
面试题:Redis是单线程,速度为什么会这么快?
查看>>
关于String==和String.intern()的面试题,一文读懂
查看>>
new Hashmap 和 new ArrayList时设置初始化容量多少合适
查看>>
java面试中面试官让你讲讲反射,应该从何讲起?
查看>>
RocketMQ概念简介
查看>>
关于BIO和NIO的理解与总结(网络IO)
查看>>
STL应用之stack、queue、priority_queue容器适配器
查看>>
继承的学习——C++
查看>>
实现一个minishell小程序
查看>>
设计模式(单例模式)——Linux系统编程
查看>>
网络基础1(协议、协议模型、IP、Port、网络字节序)——Linux网络编程
查看>>
网络基础2(ARP、NAT、DNS协议)——Linux网络编程
查看>>
UDP、TCP协议——Linux网络编程
查看>>
HTTP、HTTPS协议——Linux网络编程
查看>>
string类——C++
查看>>
SpringMVC入门(springMVC的环境配置和入门程序以及简单的流程)
查看>>
PigyChan_LeetCode 725. 分隔链表
查看>>
PigyChan_LeetCode 2. 两数相加
查看>>