gpt4 book ai didi

c++ - 显示未聚焦的Qt窗口

转载 作者:行者123 更新时间:2023-12-02 10:28:43 25 4
gpt4 key购买 nike

如何使用Qt创建一个可以与用户进行交互的窗口(即单击窗口中的按钮),但不会改变或获得焦点?
用例:我有一个QLineEdit,当QLineEdit获得关注时,我想显示一个额外的“操纵器”弹出窗口。该操纵器弹出窗口可能包含用于诸如转换为大写/小写等任务的按钮。
如果将操纵器的所有子控件的焦点策略都设置为Qt::NoFocus,我就可以使它起作用,但是这有点奇怪,因为我实际上必须对其进行迭代。
我想知道是否有一种方法可以完全禁用对窗口小部件的聚焦,例如setEnabled()将禁用/启用所有子窗口小部件。
以下是到目前为止的一些发现。
QCompleterQCompleter的功能与QLineEdit非常相似,显示QListView弹出窗口。通过将QListView的parent设置为0来使其成为窗口,并设置Qt::Popup窗口标志以使其成为无框架,从而实现了此目的。最后,它设置focusPolicy(Qt::NoFocus)以防止其从关联的QLineEdit接管焦点。
这很简单,因为只有一个小部件需要将其焦点策略设置为Qt::NoFocus
QDateTimeEdit
QDateTimeEdit中,完成了类似的操作。但是,那里的弹出窗口实际上是焦点。

最佳答案

开箱即用的窗口小部件不存在。但是,可以通过以下方式完成:
头文件:

#include <QObject>
#include <QWidget>
#include <QLineEdit>
#include <QBoxLayout>
#include <QPushButton>
#include <QFocusEvent>

class LineEdit : public QLineEdit
{
Q_OBJECT
public:
LineEdit(QWidget *parent) : QLineEdit(parent)
{ }

void focusInEvent(QFocusEvent *event) override
{
QLineEdit::focusInEvent(event);
emit focusIn();
}

void focusOutEvent(QFocusEvent *event) override
{
QLineEdit::focusOutEvent(event);
emit focusOut();
}

signals:
void focusIn();
void focusOut();
};

class ExtendedLineEdit : public QWidget
{
Q_OBJECT
public:
explicit ExtendedLineEdit(QWidget *parent = nullptr);

protected:
bool eventFilter(QObject *obj, QEvent *event) override;

private:
void setupWidget();

LineEdit *lineEdit;
QWidget *showableWidget;
QPushButton *toUppercaseButton;
QPushButton *toLowercaseButton;

QWidget *parent;

};
源文件:
#include "ExtendedLineEdit.h"
#include <QPoint>

ExtendedLineEdit::ExtendedLineEdit(QWidget *parent)
: QWidget(parent)
, parent(parent)
{
setupWidget();
}

bool ExtendedLineEdit::eventFilter(QObject *obj, QEvent *event)
{
Q_UNUSED(obj)
if (event->type() == QEvent::MouseButtonRelease ||
event->type() == QEvent::MouseButtonPress ||
event->type() == QEvent::MouseButtonDblClick)
{
lineEdit->setFocus();
}
return false;
}

void ExtendedLineEdit::setupWidget()
{
auto mainLayout = new QBoxLayout(QBoxLayout::Direction::TopToBottom, this);
this->setLayout(mainLayout);

lineEdit = new LineEdit(this);
mainLayout->addWidget(lineEdit);

showableWidget = new QWidget(parent);
showableWidget->setAutoFillBackground(true);
showableWidget->setPalette(QPalette(QPalette::Background, Qt::white));
showableWidget->hide();

auto bottomLayout = new QBoxLayout(QBoxLayout::Direction::LeftToRight, showableWidget);
showableWidget->setLayout(bottomLayout);

toUppercaseButton = new QPushButton("to uppercase", this);
toUppercaseButton->installEventFilter(this);
connect(toUppercaseButton, &QPushButton::clicked,
this, [&]{
lineEdit->setText(lineEdit->text().toUpper());
});

toLowercaseButton = new QPushButton("to lowercase", this);
toLowercaseButton->installEventFilter(this);
connect(toLowercaseButton, &QPushButton::clicked,
this, [&]{
lineEdit->setText(lineEdit->text().toLower());
});

bottomLayout->addWidget(toUppercaseButton);
bottomLayout->addWidget(toLowercaseButton);

connect(lineEdit, &LineEdit::focusIn,
this, [&]{
auto size = lineEdit->size();
auto position = QWidget::mapTo(parent, lineEdit->pos());

showableWidget->setGeometry(position.x(),
position.y() + size.height(),
size.width(),
40);
showableWidget->show();

});

connect(lineEdit, &LineEdit::focusOut,
showableWidget, &QWidget::hide);
}
结果:
enter image description here

关于c++ - 显示未聚焦的Qt窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63181016/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com