如何检测鼠标单击时的修饰键

发布于 2024-09-07 01:12:20 字数 156 浏览 6 评论 0原文

我有一个 QTableWidget,希望在单击列标题时按 Ctrl 标记整个列。

获取列索引不是问题,因为有一个 sectionPressed 信号,它为我提供了单击的列的当前索引。

单击列时如何获取任何键盘修饰符的状态?

I have a QTableWidget and would like that pressing Ctrl while clicking on a column header, marks the whole column.

To get the column index is not a problem, since there is a sectionPressed signal, which gives me the current index of the column clicked.

How can I get the state of any keyboard modifiers when a column is clicked?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

诗笺 2024-09-14 01:12:20

如果您想通过鼠标单击事件了解修饰键状态,可以使用 QGuiApplication::keyboardModifiers() 它将检索最后一个鼠标事件期间的按键状态:

if(QGuiApplication::keyboardModifiers().testFlag(Qt::ControlModifier)) {
    // Do a few things
}

否则,如果您想显式查询修饰符状态,您应该使用 QGuiApplication::queryKeyboardModifiers()。这在其他用例中可能是必需的,例如在应用程序启动期间检测修饰键。

If you are want to know the modifiers key state from a mouse click event, you can use QGuiApplication::keyboardModifiers() which will retrieve the key state during the last mouse event:

if(QGuiApplication::keyboardModifiers().testFlag(Qt::ControlModifier)) {
    // Do a few things
}

Otherwise, if you want to query explicitly the modifiers state you should use QGuiApplication::queryKeyboardModifiers(). This might be required in other use case like detecting a modifier key during the application startup.

半仙 2024-09-14 01:12:20

来自 Qt 文档 — QMouseEvent 类

键盘修饰键的状态可以通过调用继承自QInputEvent的modifiers()函数来找到。

From Qt Documentation — QMouseEvent Class:

The state of the keyboard modifier keys can be found by calling the modifiers() function, inherited from QInputEvent.

桃酥萝莉 2024-09-14 01:12:20

我必须安装 eventFilter 并删除 sectionPressed 处理程序:

ui->tableWidget->horizontalHeader()->viewport()->installEventFilter(this);

eventFilter 中,我可以检查是否按下了某个键,如下所示:

bool MainWindow::eventFilter(QObject *object, QEvent *event)
{
    if(event->type() == QEvent::MouseButtonPress)
    {
        if(Qt::ControlModifier == QApplication::keyboardModifiers())
        {
            QMouseEvent *mouseEvent = static_cast<QMouseEvent *>(event);
            if(mouseEvent)
            {
                if(mouseEvent->button()== Qt::LeftButton)
                {
                    ui->tableWidget->selectColumn(ui->tableWidget->itemAt(mouseEvent->pos())->column());
                    return true;
                }
            }
        }
    }

    return QWidget::eventFilter(object,event);
}

I have to install an eventFilter and remove the sectionPressed handler:

ui->tableWidget->horizontalHeader()->viewport()->installEventFilter(this);

Within the eventFilter, I can check whether a key was pressed like so:

bool MainWindow::eventFilter(QObject *object, QEvent *event)
{
    if(event->type() == QEvent::MouseButtonPress)
    {
        if(Qt::ControlModifier == QApplication::keyboardModifiers())
        {
            QMouseEvent *mouseEvent = static_cast<QMouseEvent *>(event);
            if(mouseEvent)
            {
                if(mouseEvent->button()== Qt::LeftButton)
                {
                    ui->tableWidget->selectColumn(ui->tableWidget->itemAt(mouseEvent->pos())->column());
                    return true;
                }
            }
        }
    }

    return QWidget::eventFilter(object,event);
}
☆獨立☆ 2024-09-14 01:12:20
if (QApplication::keyboardModifiers().testFlag(Qt::ControlModifier) == true) {
if (QApplication::keyboardModifiers().testFlag(Qt::ControlModifier) == true) {
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文