[英]How to disable control+A shortcut for QTableView and prevent select all option?
I want to prevent control+A
shortcut from selecting all the files in QTableView
, I want to make it as such that it can only select with mouse but not with a keyboard shortcut. 我想防止
control+A
快捷键选择QTableView
所有文件,我想使其只能通过鼠标选择,而不能通过键盘快捷键选择。 Right now my eventFilter
code is as below. 现在,我的
eventFilter
代码如下。 Could you please suggest me a way to do that? 您能建议我一种方法吗?
bool MainWindow::eventFilter(QObject* obj, QEvent *ev)
{
if(ev->type() == QEvent::MouseButtonPress)
{
if(obj == ui->listOfImages->viewport())
{
QMouseEvent * mouseEv = static_cast<QMouseEvent*>(ev);
if((mouseEv->buttons() & Qt::LeftButton) && (QApplication::keyboardModifiers().testFlag(Qt::ControlModifier) == true))
{
controlButtonCounter++;
fetch = true;
return QObject::eventFilter(obj,ev);
}
else if((mouseEv->buttons() & Qt::LeftButton) && (QApplication::keyboardModifiers().testFlag(Qt::ControlModifier) == false))
{
if(selectedImages.size()>0)
{
ui->listOfImages->clearSelection();
selectedImages.clear();
selectedList.clear();
ui->selectedFiles->clear();
ui->selectedFiles->show();
}
fetch = false;
controlButtonCounter = 0;
}
}
}
return QObject::eventFilter(obj,ev);
}
I would try to extend your event filter's code with the following block: 我会尝试使用以下代码块扩展事件过滤器的代码:
[..]
if (event->type() == QEvent::KeyPress && obj == ui->listOfImages->viewport()) {
QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
int key = keyEvent->key();
Qt::KeyboardModifiers modifiers = keyEvent->modifiers();
if(modifiers & Qt::ShiftModifier)
key += Qt::SHIFT;
if(modifiers & Qt::ControlModifier)
key += Qt::CTRL;
if(modifiers & Qt::AltModifier)
key += Qt::ALT;
if(modifiers & Qt::MetaModifier)
key += Qt::META;
if (QKeySequence(key) == QKeySequence(QKeySequence::SelectAll)) {
// Filter the event.
return true;
}
}
[..]
This code is supposed to return true (filter the event) if standard "select all" key combination pressed. 如果按下标准的“全选”组合键,则此代码应返回true(过滤事件)。 It is usually Ctrl+A keys.
通常是Ctrl + A键。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.