如何点击离焦小部件中的按钮而不改变当前的焦点 [英] How to click a button in a out-of-focus widget without changing the current focus to it

查看:307
本文介绍了如何点击离焦小部件中的按钮而不改变当前的焦点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



有一个查找表,用于选择用于输入的候选单词。 文本输入区到查找表和选定的单词不能被发送到文本输入区。

剂量任何人有任何想法来解决这个问题?感谢〜




这里我举一个简单的例子:

main.cpp

  #includeInputWidget.h
#includeButtonWidget.h
#包括< QApplication>
$ b $ int main(int argc,char * argv [])
{
QApplication app(argc,argv);
InputWidget * inputWidget = new InputWidget();
ButtonWidget * buttonWidget = new ButtonWidget();
inputWidget-> show();
buttonWidget-> show();
int ref = app.exec();
inputWidget-> deleteLater();
buttonWidget-> deleteLater();
return ref;



$ b InputWidget.h

  #include< QWidget> 
#include< QPlainTextEdit>

#ifndef _InputWidget_H_
#define _InputWidget_H_

class InputWidget:public QWidget
{
Q_OBJECT
public:
InputWidget(QWidget * parent = 0);
private:
QPlainTextEdit * inputArea;
};

#endif

InputWidget.cpp

  #includeInputWidget.h
#include< QPushButton>
#include< QVBoxLayout>

InputWidget :: InputWidget(QWidget * parent):QWidget(parent)
{
//输入区域设置
inputArea = new QPlainTextEdit(this);
//主布局
QVBoxLayout * mainLayout = new QVBoxLayout(this);
mainLayout-> setContentsMargins(1,4,1,1);
mainLayout-> addWidget(inputArea);
setLayout(mainLayout);



$ b $ p

$ b ButtonWidget.h

pre> #include< QWidget>
#include< QPushButton>

#ifndef _ButtonWidget_H_
#define _ButtonWidget_H_

class ButtonWidget:public QWidget
{
Q_OBJECT
public:
ButtonWidget(QWidget * parent = 0);
private:
QPushButton * selectedBtn;
public slots:
void changeBtnText();
};

#endif

ButtonWidget.cpp

  #includeButtonWidget.h
#include< QPushButton>
#include< QVBoxLayout>

ButtonWidget :: ButtonWidget(QWidget * parent):QWidget(parent)
{
// selectedBtn setup
selectedBtn = new QPushButton(tr(Click Me! !),这个);
connect(selectedBtn,SIGNAL(clicked()),this,SLOT(changeBtnText()));
//主布局
QVBoxLayout * mainLayout = new QVBoxLayout(this);
mainLayout-> setContentsMargins(1,4,1,1);
mainLayout-> addWidget(selectedBtn);
setLayout(mainLayout);



ButtonWidget :: changeBtnText()
{
selectedBtn-> setText(I am clicked :));
}

这些代码会生成一个具有PlainTextEditinputArea其中有一个PushButtonselectedBtn。

首先,我在inputArea中输入一些单词。但是,当我将鼠标移动到ButtonWidget,然后单击selectedBtn,将foucs更改为selectedBtn中的selectedBtn。 ButtonWidget。



如何点击selectedBtn但仍保留inputArea?感谢〜






就像我在laura的回答中所描述的评论,InputWidget和ButtonWidget可能没有相同的父亲,我不能使用QWidget setFocus插槽来改变它们之间的当前焦点。

解决方案

感谢laura和cjhuitt,您的回答给了我一个很大的提示来解决我的问题。

由于InputWidget和ButtonWidget是两个独立的小部件,如果我们想要处理它们之间的焦点问题,我们需要一个全局控制,即使用QApplication来完成焦点作业。



关键是使用QApplication的focusChanged插槽和QWidget的activateWindow。以下是我的修改。

main.cpp

  #include  InputWidget.h
#includeButtonWidget.h
#include< QApplication>
$ b $ int main(int argc,char * argv [])
{
QApplication app(argc,argv);
InputWidget * inputWidget = new InputWidget();
ButtonWidget * buttonWidget = new ButtonWidget(0,& app);
inputWidget-> show();
buttonWidget-> show();
int ref = app.exec();
inputWidget-> deleteLater();
buttonWidget-> deleteLater();
return ref;



$ b $ p

$ b ButtonWidget.h

pre> #include< QWidget>
#include< QPushButton>
#include< QApplication>

#ifndef _ButtonWidget_H_
#define _ButtonWidget_H_

class ButtonWidget:public QWidget
{
Q_OBJECT
public:
ButtonWidget(QWidget * parent = 0,QApplication * app = 0);
private:
QPushButton * selectedBtn;
public slots:
void changeBtnText();
void changeFocus(QWidget * oldWidget,QWidget * curWidget);
};

#endif

ButtonWidget.cpp

  #includeButtonWidget.h
#include< QPushButton>
#include< QVBoxLayout>

ButtonWidget :: ButtonWidget(QWidget * parent,QApplication * app):QWidget(parent)
{
// selectedBtn setup
selectedBtn = new QPushButton(tr Click Me !!),这个);
connect(selectedBtn,SIGNAL(clicked()),this,SLOT(changeBtnText()));
//主布局
QVBoxLayout * mainLayout = new QVBoxLayout(this);
mainLayout-> setContentsMargins(1,4,1,1);
mainLayout-> addWidget(selectedBtn);
setLayout(mainLayout);
//处理焦点
connect(app,SIGNAL(focusChanged(QWidget *,QWidget *)),this,SLOT(changeFocus(QWidget *,QWidget *)));



ButtonWidget :: changeBtnText()
{
selectedBtn-> setText(I am clicked :));
}

void
ButtonWidget :: changeFocus(QWidget * oldWidget,QWidget * curWidget)
{
if(oldWidget&& curWidget ==这个)
oldWidget-> activateWindow();
}

InputWidget.cpp和InputWidget不会被修改。 b

这个解决方案可以在这个例子中工作,但我不确定它可以在两个独立的Qt程序(他们有自己的QApplication)中工作,特别是在Qt Embedded环境中。我会继续做一些测试。如果有任何结果,我也会在这里发帖。



感谢这次讨论,我学到了很多,再次感谢大家!! b $ b

I am trying to implement a input method with Qt Embedded.

There is a lookup table for choosing the candidate words for typing. "text input area" to the "lookup table" and the selected word cannot be sent to the "text input area".

Dose anyone have any idea to solve this problem? Thanks~


Here I give a simple example:

main.cpp

#include "InputWidget.h"
#include "ButtonWidget.h"
#include <QApplication>

int main(int argc,char *argv[])
{
    QApplication app(argc,argv);
    InputWidget *inputWidget=new InputWidget();
    ButtonWidget *buttonWidget=new ButtonWidget();
    inputWidget->show();
    buttonWidget->show();
    int ref=app.exec();
    inputWidget->deleteLater();
    buttonWidget->deleteLater();
    return ref;
}

InputWidget.h

#include <QWidget>
#include <QPlainTextEdit>

#ifndef _InputWidget_H_
#define _InputWidget_H_

class InputWidget:public QWidget
{
Q_OBJECT
public:
    InputWidget(QWidget *parent=0);
private:
    QPlainTextEdit *inputArea;
};

#endif

InputWidget.cpp

#include "InputWidget.h"
#include <QPushButton>
#include <QVBoxLayout>

InputWidget::InputWidget(QWidget *parent):QWidget(parent)
{
    //input area setup
    inputArea=new QPlainTextEdit(this);
    //main layout
    QVBoxLayout *mainLayout=new QVBoxLayout(this);
    mainLayout->setContentsMargins(1,4,1,1);
    mainLayout->addWidget(inputArea);
    setLayout(mainLayout);
}

ButtonWidget.h

#include <QWidget>
#include <QPushButton>

#ifndef _ButtonWidget_H_
#define _ButtonWidget_H_

class ButtonWidget:public QWidget
{
Q_OBJECT
public:
    ButtonWidget(QWidget *parent=0);
private:
    QPushButton *selectedBtn;
public slots:
    void changeBtnText();
};

#endif

ButtonWidget.cpp

#include "ButtonWidget.h"
#include <QPushButton>
#include <QVBoxLayout>

ButtonWidget::ButtonWidget(QWidget *parent):QWidget(parent)
{
    //selectedBtn setup
    selectedBtn=new QPushButton(tr("Click Me!!"),this);
    connect(selectedBtn,SIGNAL(clicked()),this,SLOT(changeBtnText()));
    //main layout
    QVBoxLayout *mainLayout=new QVBoxLayout(this);
    mainLayout->setContentsMargins(1,4,1,1);
    mainLayout->addWidget(selectedBtn);
    setLayout(mainLayout);
}

void
ButtonWidget::changeBtnText()
{
    selectedBtn->setText("I am clicked :)");
}

Those codes would generate a widget which has a PlainTextEdit "inputArea" and a widget which has a PushButton "selectedBtn".

First, I input some words in the "inputArea". The current foucs is on "inputArea" in the InputWidget.

But when I move mouse to ButtonWidget and click the "selectedBtn", the foucs is changed to "selectedBtn" in the ButtonWidget.

How do I click the "selectedBtn" but still keep the foucs on "inputArea"? Thanks~


Just like my comment described in laura's answer, InputWidget and ButtonWidget may have no identical parent and I cannot use QWidget's "setFocus" slot to change the current focus between them.

解决方案

Thank laura and cjhuitt, your responses give me a big hint to solve my question.

Because InputWidget and ButtonWidget are two independent widgets, if we want to deal with the focus issue between them, we need a "global" control, i.e., use QApplication to do the focus job.

The key point is using QApplication's "focusChanged" slot and QWidget's "activateWindow". The following is my modification.

main.cpp

#include "InputWidget.h"
#include "ButtonWidget.h"
#include <QApplication>

int main(int argc,char *argv[])
{
    QApplication app(argc,argv);
    InputWidget *inputWidget=new InputWidget();
    ButtonWidget *buttonWidget=new ButtonWidget(0,&app);
    inputWidget->show();
    buttonWidget->show();
    int ref=app.exec();
    inputWidget->deleteLater();
    buttonWidget->deleteLater();
    return ref;
}

ButtonWidget.h

#include <QWidget>
#include <QPushButton>
#include <QApplication>

#ifndef _ButtonWidget_H_
#define _ButtonWidget_H_

class ButtonWidget:public QWidget
{
Q_OBJECT
public:
    ButtonWidget(QWidget *parent=0,QApplication *app=0);
private:
    QPushButton *selectedBtn;
public slots:
    void changeBtnText();
    void changeFocus(QWidget *oldWidget,QWidget *curWidget);
};

#endif

ButtonWidget.cpp

#include "ButtonWidget.h"
#include <QPushButton>
#include <QVBoxLayout>

ButtonWidget::ButtonWidget(QWidget *parent,QApplication *app):QWidget(parent)
{
    //selectedBtn setup
    selectedBtn=new QPushButton(tr("Click Me!!"),this);
    connect(selectedBtn,SIGNAL(clicked()),this,SLOT(changeBtnText()));
    //main layout
    QVBoxLayout *mainLayout=new QVBoxLayout(this);
    mainLayout->setContentsMargins(1,4,1,1);
    mainLayout->addWidget(selectedBtn);
    setLayout(mainLayout);
    //deal with focus
    connect(app,SIGNAL(focusChanged(QWidget*,QWidget*)),this,SLOT(changeFocus(QWidget*,QWidget*)));
}

void
ButtonWidget::changeBtnText()
{
    selectedBtn->setText("I am clicked :)");
}

void
ButtonWidget::changeFocus(QWidget *oldWidget,QWidget *curWidget)
{
    if(oldWidget && curWidget==this)
        oldWidget->activateWindow();
}

InputWidget.cpp and InputWidget are not modified.

This solution can work in this example, but I am not sure that it can work in two independent Qt programs(they have their own QApplication), especially in Qt Embedded environment. I would continue doing some tests on it. If there are any results, I would also post them here.

Thank for this discussion, I have learned a lot and thank you all again!!

这篇关于如何点击离焦小部件中的按钮而不改变当前的焦点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆