QT5学习
2015-07-17 15:24
288 查看
一、hello,word 程序:
1、安装QT5.4.2 windows 新建项目——application——按引导建立工程文件
2、更改main.cpp中的内容,如下所示:
3、单击运行 不理解标签也有show函数。。。。要显示标签必须用show
二、信号与槽机制:
QT5中的函数原型:
信号的产生与接收对象都是指针形式,信号与槽函数信号槽要求信号和槽的参数一致,所谓一致,是参数类型一致。
1、自带的信号与槽机制
为什么是QPushButton::clicked,&QApplication而不是button,与a呢,是调用类的成员函数还是对象的成员函数。。。。
2、自定义信号与槽机制
选择QT应用台控制程序,只包含一个main.cpp,无ui;并添加2个头文件
Newspaer.h文件
reader.h文件
main.cpp文件
这几段小代码,出了很多错误
注意class类的写法,有;有公有以及私有 main.cpp得包含各头文件以及类命名的问题!!
三、mainwidow添加动作Action
mainwidow.h
mainwidow.cpp
main.cpp
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();
d0bc
}
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();
}
相关文章推荐
- Qt数据库:(二)添加MySQL驱动插件
- Qt-优化布局结构
- Ubuntu14.04 LTS Qt 5.3.1下的QtCreator fcitx,ibus不能输入中文
- Qt中 QString 和int, char等的“相互”转换
- QT 获取各种路径
- QT 按钮切换背景图片
- QT自定义窗口
- QT 信号与槽connect
- qt5 mouseless 多种部件切换带焦点高亮显示
- PyQt实战之计算器的实现
- Qt笔记——MOC(莫克)
- (一) Qt Model/View 的简单说明
- Qt连接Sql server错误ConnectionOpen (Connect()). QODBC3: Unable to connect"
- pyQt不同窗体间的值传递(二)——使用信号槽机制
- Qt通过ODBC来操作Excel
- Visual Studio下Qt调用IDL
- ubuntu下QT与opencv的配置安装
- QT POST/GET 发送/获取网页数据
- qt5 mouseless 多子页面切换测试
- Qt5 mouseless 测试