gpt4 book ai didi

c++ - 在 QT 中从一个 QToolButton 切换到另一个

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:39:45 26 4
gpt4 key购买 nike

我想从一个 QToolButton 切换到 QToolBar 中的另一个。我使用过 QStackedWidget,从一个 widget 移动到另一个太简单了,但在这里我无法通过使用 keyReleaseEvent .

mywindow::mywindow() : QMainWindow()
{
widget = new QWidget();
setCentralWidget(widget);

tool = new QToolBar();
vertical = new QVBoxLayout();
button1 = new QToolButton();
connect( button1, SIGNAL(clicked()), this, SLOT(fileNew()) );

button2 = new QToolButton();
button3 = new QToolButton();

button1->setIcon(QIcon("download.jpg"));
button1->setGeometry(0,0,100,200);
button2->setIcon(QIcon("images.jpg"));
button3->setIcon(QIcon("settings-icon.jpg"));

//stack1->addWidget(button1);
//stack1->addWidget(button2);
//stack1->addWidget(button3);

tool->addWidget(button1);
tool->addWidget(button2);
tool->addWidget(button3);
//tool->addWidget(stack1);

vertical->addWidget(tool);
widget->setLayout(vertical);
}

void mywindow::keyReleaseEvent(KeyEvent *event)
{
switch(event->key())
{
case:Qt::Key_Left:


}
}

最佳答案

您需要检查焦点,并根据需要移动焦点。我会这样写:

void mywindow::keyReleaseEvent(KeyEvent *event)
{
switch(event->key())
{
case:Qt::Key_Left:
if (button3->hasFocus())
button2->setFocus();
else if (button2->hasFocus())
button1->setFocus();
break;
case:Qt::Key_Right:
if (button1->hasFocus())
button2->setFocus();
else if (button2->hasFocus())
button3->setFocus();
break;
}
}

请注意,如果您继续添加更多按钮,这段代码很容易变得单调乏味。我会把它们放在一个容器里。然后,我将根据焦点切换逻辑正向和反向遍历该容器。

有关详细信息,请参阅文档:

focus : const bool

This property holds whether this widget (or its focus proxy) has the keyboard input focus.

By default, this property is false.

Note: Obtaining the value of this property for a widget is effectively equivalent to checking whether QApplication::focusWidget() refers to the widget.

Access functions:bool hasFocus() const

关于c++ - 在 QT 中从一个 QToolButton 切换到另一个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21110515/

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