the_naive
the_naive

Reputation: 3064

Permanently disable shift in Qt when the GUI is running?

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);
}

Upvotes: 0

Views: 479

Answers (2)

dgrat
dgrat

Reputation: 2244

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

Upvotes: 3

vahancho
vahancho

Reputation: 21240

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;
}

Upvotes: 1

Related Questions