无法从Qwidget向MainWindow发送信号

时间:2015-04-06 08:11:23

标签: keyboard qt4 virtual

我已使用this example编写虚拟键盘应用程序。我已经改变了一点接受退格,左键等。 我想要做的是我想从输入面板表单向MainWindow发送一个信号(enterKeyGenerated)。但我没有这样做。这是我的代码:

MyInputPanel.cpp(编者)

MyInputPanel::MyInputPanel()
 : QWidget(0, Qt::Tool| Qt::WindowStaysOnTopHint|Qt::CustomizeWindowHint |Qt::FramelessWindowHint),
lastFocusedWidget(0)
{

form.setupUi(this);
connect(qApp, SIGNAL(focusChanged(QWidget*,QWidget*)),
         this, SLOT(saveFocusWidget(QWidget*,QWidget*)));

 connect(form.panelButton_0,SIGNAL(clicked()),this,SLOT(buttonClickedIntermediate()));
 connect(form.panelButton_1,SIGNAL(clicked()),this,SLOT(buttonClickedIntermediate()));
 connect(form.panelButton_2,SIGNAL(clicked()),this,SLOT(buttonClickedIntermediate()));
 connect(form.panelButton_3,SIGNAL(clicked()),this,SLOT(buttonClickedIntermediate()));
 connect(form.panelButton_4,SIGNAL(clicked()),this,SLOT(buttonClickedIntermediate()));
 connect(form.panelButton_5,SIGNAL(clicked()),this,SLOT(buttonClickedIntermediate()));
 connect(form.panelButton_6,SIGNAL(clicked()),this,SLOT(buttonClickedIntermediate()));
 connect(form.panelButton_7,SIGNAL(clicked()),this,SLOT(buttonClickedIntermediate()));
 connect(form.panelButton_8,SIGNAL(clicked()),this,SLOT(buttonClickedIntermediate()));
 connect(form.panelButton_9,SIGNAL(clicked()),this,SLOT(buttonClickedIntermediate()));

 connect(form.leftKeyBtn,SIGNAL(clicked()),this, SLOT(sendLeftKey()));
 connect(form.rightKeyBtn,SIGNAL(clicked()),this, SLOT(sendRightKey()));
 connect(form.bckSpcBtn,SIGNAL(clicked()),this,SLOT(sendBackSpaceKey()));

 connect(form.closeButton,SIGNAL(clicked()),this,SLOT(sendClosekey()));
 connect(form.enterBtn,SIGNAL(clicked()),this, SLOT(sendEnterKey()));
}
void MyInputPanel::buttonClickedIntermediate()
{
  QPushButton *button = (QPushButton *)sender();
  buttonClicked(button);
}
void MyInputPanel::buttonClicked(QWidget *w)
{
  QChar chr = qvariant_cast<QChar>(w->property("buttonValue"));
  emit characterGenerated(chr);
}

void MyInputPanel::sendF1Key()
{
  emit f1KeyGenerated();
}

void MyInputPanel::sendEnterKey()
{
   emit enterKeyGenerated();
}
void MyInputPanel::sendClosekey()
{
   emit closeKeyGenerated();
}

void MyInputPanel::sendBackSpaceKey()
{
   emit bckSpcKeyGenerated();
}

void MyInputPanel::sendLeftKey()
{
   emit leftKeyGenerated();
}

void MyInputPanel::sendRightKey()
{
   emit rightKeyGenerated();
}

bool MyInputPanel::event(QEvent *e)
{
   switch (e->type())
   {
      case QEvent::WindowActivate:
        if (lastFocusedWidget)
        lastFocusedWidget->activateWindow();
       break;
     default:
       break;
   }

   return QWidget::event(e);
}


void MyInputPanel::saveFocusWidget(QWidget * /*oldFocus*/, QWidget *newFocus)
{
  if (newFocus != 0 && !this->isAncestorOf(newFocus))
  {
     lastFocusedWidget = newFocus;
  }
}
MainWindow.cpp中的

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
     ui->setupUi(this);
     . . . 
     inputPanel = new MyInputPanel;
     connect(inputPanel,SIGNAL(rightKeyGenerated()),this,SLOT(setToDefault()));
     . . .
}

 void MainWindow::setToDefault()
{
    QMessageBox q;
    q.setText("yyayyy");
    q.exec();
    connect(ui->comboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(test(int)));
}

MyInputPanelContext.cpp:

 #include <QtCore>
 #include "myinputpanelcontext.h"

 MyInputPanelContext::MyInputPanelContext()
 {
   inputPanel = new MyInputPanel;
   connect(inputPanel,SIGNAL(characterGenerated(QChar)),  SLOT(sendCharacter(QChar)));
   connect(inputPanel,SIGNAL(bckSpcKeyGenerated()),SLOT(sendBackSpaceKey()));
   connect(inputPanel,SIGNAL(leftKeyGenerated()),SLOT(sendLeftKey()));
   connect(inputPanel,SIGNAL(rightKeyGenerated()),SLOT(sendRightKey()));
   connect(inputPanel,SIGNAL(enterKeyGenerated()),SLOT(sendEnterKey()));
   connect(inputPanel,SIGNAL(closeKeyGenerated()),SLOT(sendCloseKey()));
   connect(inputPanel,SIGNAL(f1KeyGenerated()),SLOT(sendF1Key()));
}


MyInputPanelContext::~MyInputPanelContext()
{
   delete inputPanel;

}


 bool MyInputPanelContext::filterEvent(const QEvent* event)
 {
     if (event->type() == QEvent::RequestSoftwareInputPanel)
     {
        updatePosition();
        inputPanel->show();
        return true;
     }
     else if (event->type() == QEvent::CloseSoftwareInputPanel)
     {
         inputPanel->hide();
         return true;
     }
     return false;
  }      


 . . . 
 void MyInputPanelContext::sendCharacter(QChar character)
 {
    QPointer<QWidget> w = focusWidget();

    if (!w)
      return;

    QKeyEvent keyPress(QEvent::KeyPress, character.unicode(), Qt::NoModifier, QString(character));

    if (!w)
      return;

    QKeyEvent keyRelease(QEvent::KeyPress, character.unicode(), Qt::NoModifier, QString());
    QApplication::sendEvent(w, &keyRelease);
}

void MyInputPanelContext::sendBackSpaceKey()
{
     if(!focusWidget()) return;
     QKeyEvent delPress(QEvent::KeyPress, Qt::Key_Backspace, Qt::NoModifier);
     QApplication::sendEvent(focusWidget(), &delPress);
     QKeyEvent delRelease(QEvent::KeyRelease, Qt::Key_Backspace, Qt::NoModifier);
     QApplication::sendEvent(focusWidget(), &delRelease);


 }
 void MyInputPanelContext::sendRightKey()
 {
    if(!focusWidget())return;
    QKeyEvent rightKeyPress(QEvent::KeyPress,Qt::Key_Right,Qt::NoModifier);
    QApplication::sendEvent(focusWidget(), &rightKeyPress);
    QKeyEvent rightKeyRelease(QEvent::KeyRelease, Qt::Key_Right, Qt::NoModifier);
    QApplication::sendEvent(focusWidget(), &rightKeyRelease);

}
void MyInputPanelContext::sendLeftKey()
{
    if(!focusWidget()) return;

    QKeyEvent leftKeyPress(QEvent::KeyPress,Qt::Key_Left,Qt::NoModifier);
    QApplication::sendEvent(focusWidget(), &leftKeyPress);
    QKeyEvent leftKeyRelease(QEvent::KeyRelease, Qt::Key_Left, Qt::NoModifier);
    QApplication::sendEvent(focusWidget(), &leftKeyRelease);

}

void MyInputPanelContext::sendEnterKey()
{
    if(!focusWidget()) return;

   QKeyEvent delPress(QEvent::KeyPress, Qt::Key_Enter, Qt::NoModifier);
   QApplication::sendEvent(focusWidget(), &delPress);
   QKeyEvent delRelease(QEvent::KeyRelease, Qt::Key_Enter, Qt::NoModifier);
   QApplication::sendEvent(focusWidget(), &delRelease);


}

void MyInputPanelContext::sendCloseKey()
{
     if(!focusWidget()) return;

     QKeyEvent delPress(QEvent::KeyPress, Qt::Key_Close, Qt::NoModifier);
     QApplication::sendEvent(focusWidget(), &delPress);
     QKeyEvent delRelease(QEvent::KeyRelease, Qt::Key_Close, Qt::NoModifier);
     QApplication::sendEvent(focusWidget(), &delRelease);
}
void MyInputPanelContext::sendF1Key()
{
     if(!focusWidget()) return;
     QKeyEvent delPress( QEvent::KeyPress, Qt::Key_F1, Qt::NoModifier);
     QApplication::sendEvent(focusWidget(), &delPress);
     QKeyEvent delRelease(QEvent::KeyRelease, Qt::Key_F1, Qt::NoModifier);
     QApplication::sendEvent(focusWidget(), &delRelease);

 }
 void MyInputPanelContext::updatePosition()
 {
     QWidget *widget = focusWidget();
     if (!widget)
        return;

     QRect widgetRect = widget->rect();
     QPoint panelPos = QPoint(widgetRect.left(), widgetRect.bottom()+ 2);//
     panelPos = widget->mapToGlobal(panelPos);
     inputPanel->move(panelPos);

}

这里必须显示带有文字的消息框&#34; yyayy&#34;。 但没有任何反应。 请帮我看看我做错了什么。谢谢。

1 个答案:

答案 0 :(得分:0)

您需要发出与rightKeyGenerated()广告连接的信号setToDefault()

您的代码中没有任何内容。

相关问题