0

I'm using a QTableView where I show a list of Icons, the user can select some icons with mouse and control key button, and I'm able to handle these selections. But I want to disable the use of shift+left mouse key over the QTableView.

Is there any way to totally disable the shift key button during the process when GUI is being run? I am able to detect the shift key pressing using the eventFilter that is installed on the viewport of QTableView, but I cannot find any way to totally make the shift key inactive when the user presses shift key and the left mouse button together.

My event filter is as below:

bool MainWindow::eventFilter(QObject* obj, QEvent *ev)
{
    if(obj == ui->listOfImages->viewport())
    {
        if(ev->type() == QEvent::MouseButtonPress)
        {
            QMouseEvent * mouseEv = static_cast<QMouseEvent*>(ev);
            if((mouseEv->buttons() & Qt::LeftButton) && (QApplication::keyboardModifiers().testFlag(Qt::ControlModifier) == true) && (QApplication::keyboardModifiers().testFlag(Qt::ShiftModifier) == false))
            {
                controlButtonCounter++;
                fetch = true;
                //I use these variables for some purposes.
                return QObject::eventFilter(obj,ev);
            }
            else if((mouseEv->buttons() & Qt::LeftButton) && (QApplication::keyboardModifiers().testFlag(Qt::ControlModifier) == false) && (QApplication::keyboardModifiers().testFlag(Qt::ShiftModifier) == false))
            {
                if(selectedImages.size()>0)
                {
                    ui->listOfImages->clearSelection();
                    selectedImages.clear();
                    selectedList.clear();
                    ui->selectedFiles->clear();
                    ui->selectedFiles->show();
                }
                fetch = false;
                controlButtonCounter = 0;
                //I use these variables for some purposes.
            }
            else if((mouseEv->buttons() & Qt::LeftButton) && (QApplication::keyboardModifiers().testFlag(Qt::ControlModifier) == false) && (QApplication::keyboardModifiers().testFlag(Qt::ShiftModifier) == true) )
            {
                qDebug()<<"Shift button pressed!";
                // Don't how to prevent shift button from selecting multiple icon.
            }
        }
    }
    return QObject::eventFilter(obj,ev);
}
the_naive
  • 2,936
  • 6
  • 39
  • 68
  • 2
    Why would you want that? Changing the way controls usually work is likely to disorient and annoy users. If you really want to, you can return false from your event filter if you detect shift press. This should prevent event from propagating to the viewport. – j_kubik Feb 18 '14 at 15:58
  • Because for some bugs in qt I'm not able to detect what icons are selected using shift+left mouse button. Hence, the best option for is to totally disable shift button on the QTableView. @j_kubik – the_naive Feb 18 '14 at 16:01
  • BTW You should return false from event filter for mouse events, not keyboard. – j_kubik Feb 18 '14 at 16:04

2 Answers2

3

Regarding your code, I believe you want to change the way you select things in the QTableView and disabling the shift-button would be just a work around.

You can disable multi-selections with:

QAbstractItemView::selectionMode(QAbstractItemView::SingleSelection);

See: http://qt-project.org/doc/qt-4.8/qabstractitemview.html#SelectionMode-enum: for more info

dgrat
  • 2,214
  • 4
  • 24
  • 46
1

I will handle mouse clicks and button state in the following way:

bool MyWidget::eventFilter(QObject *obj, QEvent *event)
{
    [..]
    if (event->type() == QEvent::MouseButtonPress ||
        event->type() == QEvent::MouseButtonRelease) {
        Qt::KeyboardModifiers modifiers = QApplication::keyboardModifiers();

        if (modifiers & Qt::ShiftModifier) {
            // Filter the event, when mouse pressed/released
            // with the shift key pressed.
            return true;
        }
    }
    [..]
    return false;
}
vahancho
  • 20,808
  • 3
  • 47
  • 55
  • I don't quite understand your point, I changed my question and added the code of my event filter, could you please look at it and say what should I do to disable Shift key when I detect it is PRESSED combined with left mouse button. – the_naive Feb 18 '14 at 16:19
  • @the_naive, if you detect condition, when your event should be filtered, you need to simply return `true` from your event filter. – vahancho Feb 18 '14 at 16:22