本文共 4283 字,大约阅读时间需要 14 分钟。
#include
qDebug()<
QMessageBox::warning(this,tr("连接提示"),tr("连接服务器成功!"),QMessageBox::Yes,QMessageBox::No);
#include
1、在.h文件的private slots:中定义方法
2、在对应的.cpp文件中实现该方法
3、在UI上拖空间,编辑信号槽,配置连接,点编辑,绿色加号添加刚定义的方法。
============================================
方式1.传统connect()函数
例如:
connect(ui->findPushBtn,SIGNAL(clicked()),this,SLOT(find()));
参数1:事件UI发送者
参数2:发送的信号
参数3:指针指向当前事件接瘦者
参数4:接收者调用处理的函数
方式2.qt函数中默认有一个on_pushButtonConnect_clicked函数,需要实现点击事件,只需要再代码中实现就可以
1)在UI文件里加入按钮,objectName 设置为pushButtonA
2)在引用的头文件(比如MyWidget.h)里加入按钮曹的声明:
public slots: void on_pushButtonConnect_clicked();
3)在调用处使用,(比如MyWidget.cpp)
void MainWindow::on_pushButtonConnect_clicked(){ qDebug("pushButtonConnect");}
(2的另一种实现方式)在ui设计界面为右击按钮转到槽
1)在ui设计界面 右击按钮—>转到槽,会自动在MyWidget.h头文件中添加private slots: void on_pushButton_clicked();
实现按钮槽函数信号的声明
同时,在MyWidget.cpp文件中自动添加
void MyWidget::on_pushButton_clicked(){ }
实现按钮槽函数信号的声明
============================================
a、头文件添加内容:
protected: virtual void keyPressEvent(QKeyEvent *ev); virtual void keyReleaseEvent(QKeyEvent *ev);
b、.cpp源文件添加内容:
#includevoid MainWindow::keyPressEvent(QKeyEvent *ev){ if(ev->key() == Qt::Key_F5) { qDebug("pushButtonConnect5"); return; } QWidget::keyPressEvent(ev);}void MainWindow::keyReleaseEvent(QKeyEvent *ev){ if(ev->key() == Qt::Key_F5) { qDebug("pushButtonCon5"); return; } QWidget::keyReleaseEvent(ev);}
代码会自动添加如下:
头文件:private slots: void on_pushBt1_clicked(); void on_pushBt2_clicked();源文件:void MainWindow::on_pushBt1_clicked(){}void MainWindow::on_pushBt2_clicked(){}
在里面添加所需要的内容:
void MainWindow::on_pushBt1_clicked(){ qDebug("pushButtonConnect1");}void MainWindow::on_pushBt2_clicked(){ qDebug("pushButtonConnect2");}
改变按键位置、
大小、 按下按键退出、延时#include "mainwindow.h"#include#include #include #include void Delay_MSec_Suspend(unsigned int msec){ QTime _Timer = QTime::currentTime().addMSecs(msec); while( QTime::currentTime() < _Timer );}int main(int argc, char *argv[]){ QApplication a(argc, argv); MainWindow w; w.show(); //QT里面 控件 都是继承了 QWidget QPushButton hello( "Hello world!"); //创建一个名为hello的按钮 hello.setText("abc123");//重新命名 hello.resize( 100, 100 );//按键大小 Delay_MSec_Suspend(2000); hello.move(50,50); //将按钮b1移动到点(50,50)的位置 Delay_MSec_Suspend(2000); //setGeometry(int ax, int ay, int aw, int ah) hello.setGeometry(400, 200, 400, 300); //既设置大小,又设置位置 Delay_MSec_Suspend(2000); // 信号和槽的机制 // 点击 b1 按钮, 窗口 w 关闭,涉及到两个对象之间的交互 // 1、信号的发送者 // 2、发送的信号 -----> 信号 // 3、信号的接收者 // 4、接收者要做的处理 -----> 槽 QObject::connect(&hello, SIGNAL(clicked()), &w, SLOT(close())); QObject::connect(&hello, SIGNAL(clicked()), &hello, SLOT(close())); // 父子对象设置 窗口在进行展示的时候,会将它的所有子对象一起显示 // hello.setParent(&w); //w.show(); return a.exec();}
点hello退出
#include "mainwindow.h"#include#include #include int main(int argc, char *argv[]){ QApplication a(argc, argv); MainWindow w; w.show(); //QT里面 控件 都是继承了 QWidget QPushButton hello( "Hello world!"); //创建一个名为hello的按钮 hello.setText("Hello"); //重新命名 hello.setParent(&w); //相对于窗口位置 hello.resize( 100, 50 ); //按键大小 //setGeometry(int ax, int ay, int aw, int ah) hello.setGeometry(20, 20, 100, 30); //既设置大小,又设置位置 hello.show(); // 信号和槽的机制 // 点击 b1 按钮, 窗口 w 关闭,涉及到两个对象之间的交互 // 1、信号的发送者 // 2、发送的信号 -----> 信号 // 3、信号的接收者 // 4、接收者要做的处理 -----> 槽 QObject::connect(&hello, SIGNAL(clicked()), &w, SLOT(close())); QObject::connect(&hello, SIGNAL(clicked()), &hello, SLOT(close())); return a.exec();}
1、mainwindow.cpp:18:5: error: unknown type name 'string'
报错了,提示”无法启动此程序,因为计算机中丢失Qt5Cored.dll.尝试重新安装该程序以解决此问题“。那还不好办,找文件拷贝过去呗,于是到C:\Qt\Qt5.0.2\5.0.2\mingw47_32\bin,找文件开始拷贝,缺什么拷贝什么。考着考着,怕了,文件到大了,这个不行。开始找资料,哦,原来这是调试模式,不是release,错了就改嘛。
转载地址:http://pduub.baihongyu.com/