• 6550阅读
  • 6回复

事件问题   父窗口部件与子窗口部件问题 [复制链接]

上一主题 下一主题
离线myq_t
 
只看楼主 倒序阅读 楼主  发表于: 2010-02-09
////////////////////////////////////////////////// main.cpp /////////////////////////////////////////////////////////////////
 #include"KDlg.h"

 int main(int argc,char* argv[])

 { 

  QApplication app(argc,argv);

  KDialog kdlg; ////////////////////////////第一个疑问点点点点点点点点点点点点点点点点点

  kdlg.resize(400, 300);

  kdlg.setWindowTitle(QObject::tr("KDialog"));

  kdlg.show();

return app.exec();

 }


 ////////////////////////////////////////////////////////// KDlg.h//////////////////////////////////////////////////////////////
 #ifndef KDLG_H

 #define KDLG_H


 #include <QApplication>
 #include <QDialog>

 #include <QPushButton>

 #include <QKeyEvent>


 class KDialog:public QDialog
 {

  Q_OBJECT

 public:

  KDialog();

 private:

  QPushButton *KBtn1;

QPushButton *KBtn2;

 protected:

  void keyPressEvent(QKeyEvent *event);

 };


 #endif // KDLG_H

 ////////////////////////////////////////////////////////////////////////////// KDlg.cpp ////////////////////////////////////////////////////////////////////////////
 #include"KDlg.h"


 KDialog::KDialog()
 {

  KBtn1 = new QPushButton(this); /////////////////////////////////第一个疑问点点点点点点点点点点点点点点点点点

  KBtn1->setGeometry(50,50,85,27);

  KBtn1->setText("BTN1111111");


  KBtn2 = new QPushButton(this);
  KBtn2->setGeometry(150,50,85,27);

  KBtn2->setText("BTN2222222");

 }


 void KDialog::keyPressEvent(QKeyEvent *event)
 {

  if(event->key() == Qt::Key_F1)

  {

  KBtn1->setText("KEYPRESS111111111");

  KBtn2->setText("KEYPRESS222222222");

  }

 }


 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
 //////////////////////////////////////////////////////////大家帮我看看,我对下面的现象不是很明白,哪位帮我解读解读,谢谢啦!

 ///实现同一种效果,上面是第一种方式,下面是第二种方式。第一种方式父窗口是自定义类的对象,第二种方式父窗口部件是QDialog

 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

 /////////////////////////////////////////////////////////////////// main.cpp /////////////////////////////////////////////////////////////////////////////////////////

 #include"KDlg_parent.h"


 int main(int argc,char* argv[])
 {

  QApplication app(argc,argv);

  QDialog* dlg = new QDialog;

  KDialog kdlg(dlg); /////////////////////////////////第一个疑问点点点点点点点点点点点点点点点点点

  dlg->resize(400, 300);

  dlg->setWindowTitle(QObject::tr("KDialog"));

  dlg->show();

  return app.exec();

 }


 ///////////////////////////////////////////////////////////////// KDlg_parent.h ///////////////////////////////////////////////////////////////////////////////////////
 #ifndef KDLG_PARENT_H

 #define KDLG_PARENT_H


 #include <QApplication>
 #include <QDialog>

 #include <QKeyEvent>


 class KDialog:public QDialog
 {

  Q_OBJECT

 public:

  KDialog(QDialog* = 0);

 private:

  QPushButton *KBtn1;

  QPushButton *KBtn2;

 protected:

  bool eventFilter(QObject *target, QEvent *event);

 };


 #endif // KDLG_PARENT_H

 /////////////////////////////////////////////////////////////////////// KDlg_parent.cpp /////////////////////////////////////////////////////////////////////////
 #include"KDlg_parent.h"


 KDialog::KDialog(QDialog* parent):QDialog(parent)
 {

  KBtn1 = new QPushButton(parent); ///////////////////////////////第一个疑问点点点点点点点点点点点点点点点点

  KBtn1->setGeometry(50,50,85,27);

  KBtn1->setText("BTN1111111111");


  KBtn2 = new QPushButton(parent);
  KBtn2->setGeometry(150,50,85,27);

  KBtn2->setText("BTN2222222222");


  KBtn2->installEventFilter(this); ////////////////////////////////安装事件过滤器
 }


 bool KDialog::eventFilter(QObject *target, QEvent *event)
 {

  if(target == KBtn2)

  {

  if(event->type() == QEvent::KeyPress)

  {

  QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);

  if(keyEvent->key() == Qt::Key_F1)

  {

  KBtn2->setText("KEYPRESS111111111");

  KBtn2->setText("KEYPRESS222222222");

  }

  return true;

  }

  }

  return QDialog::eventFilter(target,event);

 }


 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
 我不明白的问题有几个:

 一、第一种方法 KBtn1 = new QPushButton(this); 中 this 意思是父窗口是当前 KDialog 对象,第二种方法 KBtn1 = new QPushButton(parent); 中的 parent 意思是父窗口是QDialog的对象。我的解释有没有错?如果没有错为什么第二种方法中的 parents 不能用 this (即父窗口是 main() 函数中创建的 QDialog 对象)?哪种方法好(也就是
要不要 QDialog 作为父窗口)?
 二、为什么第二个工程的事件不能用重新实现 keyPressEvent() 的方法,而只能用安装事件过滤器的方法?

 三、为什么第二种方法中只有当 KBtn2 获得焦点后(运行之初要通过方向键将焦点移至 KBtn2 )按键盘 F1 键才能响应键盘事件? 

有什么方法能一运行 按 F1 键就能响应键盘事件?

 谢谢朋友的回答!!! 


[ 此帖被myq_t在2010-02-09 13:23重新编辑 ]
离线steinlee

只看该作者 1楼 发表于: 2010-02-09
Re:事件问题   父窗口部件与子窗口部件问题
always use heap memory for Qt component:
KDialog * kdlg = new KDialog
Qt has garbage collector. If you do not do it,
you may have memory problem.

Second way is unnecessary because KDialog is a dialog. You do not
need to define a parent dialog for it in your case.

KBtn1 = new QPushButton(this) should be fine.
Looking for remote C/C++ and Qt 兼职
离线myq_t
只看该作者 2楼 发表于: 2010-02-09
Re:Re:事件问题   父窗口部件与子窗口部件问题
引用第1楼steinlee于2010-02-09 01:20发表的 Re:事件问题 父窗口部件与子窗口部件问题 :
always use heap memory for Qt component:
KDialog * kdlg = new KDialog
Qt has garbage collector. If you do not do it,
you may have memory problem.
.......


好的,谢谢 steinlee  !!!
恕小弟愚钝,只从您的解答中看到了第一个解答。

已经有了第一个的解答:不需要 QDialog 作为父窗口。

还有两个.......
离线myq_t
只看该作者 3楼 发表于: 2010-02-09
没人回应哦,,,,,,自己顶,,,,,,
离线myq_t
只看该作者 4楼 发表于: 2010-02-09
还是没有人哦,,,,,,再等,,,,,,
离线steinlee

只看该作者 5楼 发表于: 2010-02-10
Re:事件问题   父窗口部件与子窗口部件问题
In the second method KDialog has a parent which may catch the key event. Therefore,
you need a filter for catching this? Guess. Anyway, You may never need the second way.
Looking for remote C/C++ and Qt 兼职
离线myq_t
只看该作者 6楼 发表于: 2010-02-10
Re:Re:事件问题   父窗口部件与子窗口部件问题
引用第5楼steinlee于2010-02-10 06:12发表的 Re:事件问题 父窗口部件与子窗口部件问题 :
In the second method KDialog has a parent which may catch the key event. Therefore,
you need a filter for catching this? Guess. Anyway, You may never need the second way.


谢谢steinlee!!!
好的,的意见不错,可以不采用第二种方式。
但我还想问问你,你说的第二种方法 KDialog 的父窗口可以捕获键盘事件,如何做才能让其父窗口捕获键盘事件呢?
快速回复
限100 字节
 
上一个 下一个