QT5学习
来源:互联网 发布:电力拖动仿真软件 编辑:程序博客网 时间:2024/05/22 15:13
一、hello,word 程序:
1、安装QT5.4.2 windows 新建项目——application——按引导建立工程文件
2、更改main.cpp中的内容,如下所示:
#include "mainwindow.h"
#include <QApplication>
#include<QLabel>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
/* MainWindow w;
w.show();*/
QLabel label("hello,world");
label.show();
return a.exec();
}3、单击运行 不理解标签也有show函数。。。。要显示标签必须用show
二、信号与槽机制:
QT5中的函数原型:
信号的产生与接收对象都是指针形式,信号与槽函数信号槽要求信号和槽的参数一致,所谓一致,是参数类型一致。
1、自带的信号与槽机制
#include "mainwindow.h"
#include <QApplication>
#include<QPushButton>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QPushButton button("quit");
QObject::connect(&button,#include "mainwindow.h"#include <QApplication>#include<QPushButton>int main(int argc, char *argv[]){QApplication a(argc, argv);QPushButton button("quit");QObject::connect(&button,&QPushButton::clicked,&QApplication::quit);button.show();
return a.exec();}
button.show();
return a.exec();
}
为什么是QPushButton::clicked,&QApplication而不是button,与a呢,是调用类的成员函数还是对象的成员函数。。。。
2、自定义信号与槽机制
选择QT应用台控制程序,只包含一个main.cpp,无ui;并添加2个头文件
Newspaer.h文件
#include<QObject>
class Newspaper:public QObject
{
Q_OBJECT
public:
Newspaper(const QString &name):m_name(name)
{}
void send()
{
emit newsPaper(m_name);
}
signals:
void newsPaper(const QString &name);
private:
QString m_name;
};reader.h文件
#include<QObject>
#include<QDebug>
class Reader:public QObject
{
public:
Reader()
{}
void receiveNewspaper(const QString &name)
{
qDebug()<<"Receive Newspaper:"<<name;
}
};main.cpp文件
#include <QCoreApplication>
#include<newspaper.h>
#include<reader.h>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
Newspaper paper("newspaperA");
Reader reader;
QObject::connect(&paper,&Newspaper::newsPaper,&reader,&Reader::receiveNewspaper);
paper.send();
return a.exec();
}这几段小代码,出了很多错误
注意class类的写法,有;有公有以及私有 main.cpp得包含各头文件以及类命名的问题!!
三、mainwidow添加动作Action
mainwidow.h
#include <QMainWindow>
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = 0);
~MainWindow();
private:
void open();
QAction *openAction;//槽函数为私有成员
};mainwidow.cpp
#include "mainwindow.h"
#include <QApplication>
#include<QAction>
#include<QMenuBar>
#include<QMessageBox>
#include<QStatusBar>
#include<QToolBar>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
setWindowTitle(tr("main window"));
openAction=new QAction(QIcon(":/images/test"),tr("&Open..."),this);//&表示下划线
openAction->setShortcuts(QKeySequence::Open);//快捷键
openAction->setStatusTip(tr("Open an existing file"));//鼠标滑过时,状态栏有显示信息
connect(openAction,&QAction::triggered,this,&MainWindow::open);//思考这个机制是否能够实现列表式的动作,每个动作打开一个新的窗口,从而实现不同的功能!
QMenu *file=menuBar()->addMenu(tr("&File"));
file->addAction(openAction);
QToolBar *toolbar=addToolBar(tr("&File"));
toolbar->addAction(openAction);
statusBar();//menuBar()、toolBar()和statusBar()菜单栏、工具栏和状态栏
}
MainWindow::~MainWindow()
{
}
void MainWindow::open()
{
QMessageBox::information(this,tr("Information"),tr("open"));
}main.cpp
#include "mainwindow.h"
#include<QApplication>//记得添加QApplication类
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow win;
win.show();
return a.exec();
}
0 0
- QT5学习
- QT5学习
- QT5 学习之路07---QT5事件
- qt5之学习QString
- Qt5学习笔记
- 学习Qt5(1)
- 学习Qt5(2)
- 学习Qt5(3)
- 学习Qt5(4)
- 学习Qt5(5)
- 学习Qt5(6)
- 学习Qt5(7)
- 【QT5学习历程】
- 学习Qt5(8)
- 学习Qt5(9)
- 学习Qt5(11)
- 学习Qt5(12)
- QT5学习:字符串测试
- ios中纯手写代码怎么实现控件的自动布局(约束)
- 虚拟主机根目录下的web.config配置常见问题
- 卡片游戏(Throwing cards away I,UVa 10935)
- ExtJS学习心得--第一章ExtJS而引起我工作4年多的感想
- 理解5种网络IO模型
- QT5学习
- 数次统计
- iOS开源项目 Cell滑动效果
- Web性能测试术语整理
- 极光推送学习笔记
- 活动安排-动态规划算法
- javascript中对变量类型的判断
- 报错,但不影响运行ERROR: JDWP Unable to get JNI 1.2 environment, jvm->GetEnv() return code = -2
- centos下sentinel主从监控配置