自学qt之 事件
来源:互联网 发布:淘宝评论如何上传视频 编辑:程序博客网 时间:2024/05/22 14:18
QODBC 在微软下
QMTSQL 在linux
事件触发导致了 信号的产生
事件时比较底层的概念
信号和槽是qt自己对应用程序管理的概念
protected:
void ketPressevent
event->ignore(0;//忽略掉事件信号 那么这个信号会让父对象来处理
if(event->key()=Qt::key_m)
void mylineed:keyPressEvent(QKeyEvent *event)
键盘按下,单击事件,滚轮事件
找一个空间,然后设置布局,然后删除按钮
在构造函数里写东西
{ myButton=new Mybutton;
myButton->setText(tr(“hello”));
ui->horizontalLayout->addWidget(myBUtton);
connect(myButton,SIGNAL(clicked()),this,SLOT(myBUttonClick()));
}
private slots:
void myButtonClicked();
void Dialog::myButtonClick()
{
QMessageBox:information(this,tr(“test”));
}
//pushButton里面有很多事件,他们都是受保护的虚函数 重写虚函数#include <QPushButton>class Mybutton:public QPushButton{protected:}void MyButton::keyPressEvent(QKeyEvent *){ QMessageBox::information(this,tr("test"));}
按钮信号 clicked()
键盘按下—-keyPressEvent
键盘松开—-keyReleaseEvent
鼠标按下—-mousePressEvent
鼠标松开—–mouseReleaseEvent
窗口显示—–paintEvent
定时器——–timerEvent
protected:
void mousePressEvent(QMouseEvent *e);
void mouseMoveEvent(QMouseEvent *e);
void mouseReleaseEvent(QMouseEvent *e);
void mouseDoubleClickEvent(QMouseEvent *e);
void MyWidget::paintEvent(QPaintEvent *)
{
QPainter painter(this);
QPixmap pix;
oix.load(“文件路径”);
painter.drawPixmap(pix);
}
事件处理方法:
1.重新实现事件处理器
2.重新实现QObject::event()
3.在QObject中安装事件过滤器
4.在QAppliction对象中安装事件过滤器
5.子类话QAppliction并且重新实现notify()
事件处理 :
事件处理链
当前对象–>父对象–>
事件的接收与忽略
accept()
ignore()
//构造函数里
在MainWindow::MainWindow(QWidget *parent):QMainWindow(parent)
{
QTabWidget *tabWidget=new QTabWidget;//选项卡
QWidget *firstTab,*seconed;
firstTab=new QWidget;
seconed=new QWidget;
QGroupBox *groupBox=new QGroupBox(tr(“Demo Group”),firstTab);//指定父对象
groupBox->serGeometry(100,100,300,200);//设置大小
QCheckBox *checkBox=new QCheckBox(tr(“Demo checkBox”),groupBox);//指定父对象
checkBox->setGeometry();//设置的大小
tabWidget->addTab(firstTab,tr(“First Tab”))
tabWidget->addTab(seconed,tr(“Seconed Tab”));
this->setCentralWidget(tabWidget);//作为中央窗口部件
}
//只有继承了才可以重写事件处理函数
classs MyTabWidget:public QTabWidget//继承选项卡
{
}
class MyTab:public QWidget
{
}
class MyGroupBox :public QGroupBox
{
public :
explicit MyGroupBox(const QString &title,QWidget *parent=0):QGroupBox(title ,parent){}
protected:
void mousePressEvent(QMouseEvent *event)
{
QMessageBox::information(this,tr(“test”,tr()));
event->ignore();//忽略事件会转向父对象来进行处理
event->accept();不会传到父对象
}
}
//主窗口的关闭事件
class MainWindow:public QMainWindow
{
private:
bool continueToClose()
{
if(QMessageBox::question(this,tr(“Quit”),
tr(”Are you sure “),
QMessageBox::Yes|QMessageBox::NO,
QMessageBox::NO)=QMessageBox::Yes)//默认的是NO
{
return true;
}
else
{
return false;
}
}
protected:
void closeEvent(QCloseEvent *event)
{
if(continueToclose())
{
event->accept();
}
else
{
event->ignore();
}
}
}
事件过滤器
——-事件—-过滤器—–PushButton
使用installEventFilter()安装事件过滤器
事件过滤器的eventFilter()函数
//QlineEdit 实现功能::敲空格来转换光标位置 //在构造函数里 Dialog里面有事件过滤器的函数的 Dialog::Dialog(QWidget *parent): { ui->nameLineEdit->installEventFilter(this); ui->IDLineEdit->installEventFilter(this); ui->phoneNumberLineEdit->installEventFilter(this); } bool Dialog::eventFilter(QObject *target QEvent *event)if(target==ui->nameLineEdit||traget==IDLineEdit||target==ui->phoneNUmberLineEdit) { if(event->type()==QEVent::KeyPress) { QKEyEvent *keyEvent=(QKeyEvent *)event; if(KeyEvent->key()==Qt::Key_Space) { focusNextChild();//焦点到下一个 return true; } } } return QDialog::eventFilter(target,event);//继承下来的,可能有很多功能 ,这样可以只改变该该改变的功能 }
可以安装多个过滤器
一个对象可以安装多个过滤器
先执行最后安装的过滤器
QApplication的事件过滤器
QApplication:exec() –事件队列 –分派 –pushbutton
两种QPushButton::event()
keyPressEvent()
KeyReleaseEvent()
mousePressEvent()………..
还有QMainWindow::event()
keyPressEvent()
KeyReleaseEvent()
mousePressEvent()…..
会对所有的执行过滤器
class Demofilter
{
eventfilter();
}
a.installEventFilter(new DemoFilter())
class MyApplication:public QApplication
{
bool notify(QObject ,QEVent );
{
}
}
重新实现QObject::event()
QPushButton::event()
重新实现event()
**在事件到达特定的事件处理器之前进行处理
** 使用情形
覆盖Tab键的默认功能
没有特定事件处理器的 事件
自定义事件
bool QWidget::event(QEvent *event)
{
switch(e->type())
{
case:QEvent::keyPress:
keyPressEvent((QKeyEvent*)event);
if(!(QKeyEvent *)event)->isAccepted())
retrun false;
break;
case:QEvent::KeyReleasse:
KeyReleaseEvent((QKeyEvent *)event);
……..
}
}
protected:
bool event(QEvent *e);
bool MyTextEdit::event(QEvent *e)
{
if(e->type()==QEvent::keyPress)
{
QkeyEvent keyEvent=(QkeyEvent )e;
if(KeyEvent->key()==Qt::Key_Tab)
{
return false;
}
}
retrun QTextEdit::event(e);
}
定时器事件
timerEvent()
startTimer(),killTimer()
private slots;
void beginTimer()
void endTimer()
protected:
void timerEvent(QTimerEvent *);
void paintEvent(QPaintEvent *);
void Widget::paintEvent(QPainEvent *)
{
QPainter painter(this);
painter.drawText(100,50,QString::number(count));//把数子变成字符串显示
}
void Widget::timeEvent (QTimeEvent *)
{
if(event->timerId())
count++;
update();
}
void Widget::beginTimer()
{
myTimerID= startTimer(500);
yourTimerId=startTimer(2000);
}
killTimer(myTimerId)
- 自学qt之 事件
- 自学QT之鼠标事件
- 自学QT之键盘事件
- 自学QT之事件过滤
- 自学QT之QML实现响应鼠标和键盘事件
- 自学QT之QMutableListIterator
- 自学QT之QStringListModel
- 自学QT之进度条
- 自学qt之QPainter
- 自学qt之QImage
- 自学QT之停靠窗口
- 自学QT之堆栈窗口
- 自学QT之数字时钟
- 自学QT之启动画面
- 自学QT之static_cast<int>
- 自学QT之托盘图标
- 自学QT之托盘菜单
- 自学QT之歌曲列表
- tomcat异常:Illegal access: this web application instance has been stopped already. Could not load ...
- 关于 struts2 Unable to load configuration.
- 用U盘安装Ubuntu系统,但不知道如何设置U盘启动
- Oracle索引与视图的创建与应用
- django 中验证码登录验证的实现
- 自学qt之 事件
- 【面试题20】顺时针打印矩阵
- 最长公共子序列问题
- Centos7.3.1611安装mysql5.7.18 rpm教程
- POJ 2492 A Bug's Life 带权并查集!
- SpringMVC框架
- java-RSA加密解密,支持分段加解密
- android上传文件至服务器(android端+服务器端)
- Hammer.js 进行图片缩放