linux Qt学习笔记

Posted Preference for stars

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了linux Qt学习笔记相关的知识,希望对你有一定的参考价值。

C++环境

vi *.cpp

#include <iostream>
using namespace std;
int mian()

	cout << "Hello, World!" << endl;
	return 0;


g++ *.cpp -o *App

C++基础

输入输出

cout << x << y << endl;

cin >> x >> y;

命名空间namespace

有些名字容易冲突,所以会使用命名空间的方式进行区分,具体来说就是加个前缀。

#include <iostream>
using namespace std;
namespace A

	int x = 1;
	void fun() 
		cout << "yajoer namespace" << endl;
	

using namespace A; /* 使用命名空间A */
int main()

	fun();
	A::x = 3; /* 使用命名空间A中的x变量 */
	cout << A::x << endl;
	A::fun();
	return 0;

类和对象

#include <iostream>
#include <string>
using namespace std;

class Dog

public:
	string name;
	int age;
	
	void run() 
		cout<<"The dog name is:"<<name<<"age is:"<<age<<endl;
	
; /* 注意加冒号 */

int main()

	Dog dog1;
	dog1.name = "dolar";
	dog1.age  = 8;
	dog1.run();

	Dog *dog2 = new Dog():
	dog2->name = "wangcai";
	dog2->age  = 2;
	dog2->run();
	
	delete dog2;
	dog2 = NULL;
	return 0;

构造函数和析构函数

构造函数在对象实例化时被系统自动调用,仅且调用一次。
对象生命周期结束时才会执行析构函数,仅且调用一次。

#include <iostream>
#include <string>
using namespace std;

class Dog

public:
	Dog();  /* 构造函数 */
	~Dog(); /* 析构函数 */
;

int main()

	Dog dog1; /* 对象Dog实例化 */
	cout<<"构造与析构函数示例"<<endl;
	return 0;


Dog::Dog() /* 构造函数 */

	cout<<"构造函数执行!"<<endl;

Dog::~Dog() /* 析构函数 */

	cout<<"析构函数执行!"<<endl;

this指针在

C++中,this 指针是指向类自身数据的指针,简单的来说就是指向当前类的当前实例对象。

#include <iosrteam>
#include <string>
using namespace std;
class Dog

pubilc:
	string name;
	void func();
;

int main()

	Dog dog1;
	dog1.func();
	return 0;


void Dog::func()

	this->name = "dolar";
	cout<<"The dog name is"<<this->name<<endl;

继承

新建的类继承了一个已有的类的成员

#include <iosrteam>
#include <string>
using namespace std;
class animal

public:
	string color;
	int weight;
;

class Dog : public animal

pubilL
	string name;
	int age;
	void run();
;

int main()

	Dog dog1;
	dog1.color = "black";
	return 0;

重载

函数重载

在同一个作用域内,可以声明几个功能类似的同名函数,但是这些同名函数的形式参数(指参数的个数、类型或者顺序)必须不同。我们不能仅通过返回类型的不同来重载函数。

#include <iosrteam>
#include <string>
using namespace std;
class people

public:
	string name;
	void getWeight(int weight) 
		cout<<name<<"'s weight is"<<weight<<"Kg"<<endl;
	
	void getWeight(double weight) 
		cout<<name<<"'s weight is"<<weight<<"Kg"<<endl;
	
;
int main()

	people xiaoming;
	xiaoming.getWeight(10);
	xiaoming.getWeight(10.5);
	return 0;

运算符重载

应用比较少

多态

C++多态意味着调用成员函数时,会根据调用函数的对象的类型来执行不同的函数。
形成多态必须具备三个条件:

  1. 必须存在继承关系;
  2. 继承关系必须有同名虚函数(使用关键字 virtual 声明的函数)
  3. 存在基类类型的指针或者引用,通过该指针或引用调用虚函数。
#include <iosrteam>
#include <string>
using namespace std;
class Animal

public:
	Virtual void run()  /* 虚函数 */
	cout<<"Animal's run()"<<endl;
	
;
class Dog

public:
	void run() 
	cout<<"Dog's run()"<<endl;
	
;
int mian()

	Animal *animal;
	Dog dog1;
	animal = &dog1;
	animal->run();
	return 0;

数据抽象

接口(抽象类)

初识Qt

安装Qt

wget http://download.qt.io/archive/qt/5.12/5.12.9/qt-opensource-linux-x64-5.12.9.run
chmod +x *.run
sudo ./*.run

组件除android都选

Qt简单使用

.pro 是项目管理文件
第一次编译时会报“Cannot find -lgL”的错误

sudo apt-get install libglu1-mesa-dev

使用Qt Designer 开发

点击信号槽连接的按钮如下,如下图①处,点击进入信号槽连接模式(若想退出信号槽连接模式,则点击①处左边的按钮),进入信号与槽的连接模式后,将鼠标选中我们的“关闭程序”按钮,按住按钮,然后用鼠标向外拖动,如②处。此时就会出现信号槽连接的符号。



返回到 mainwindow.cpp 找到on_pushButton_clicked 这个槽函数里。在这个槽数里写上this->close();调用 close()方法关闭整个程序。

以上是关于linux Qt学习笔记的主要内容,如果未能解决你的问题,请参考以下文章

linux Qt学习笔记

linux Qt学习笔记

Qt学习笔记-安装phonon模块

学习笔记类目录一览--不定期更新

Qt学习笔记窗口部件整理

QT学习笔记6