ฉันใช้ QTableView
เพื่อแสดงภาพบางภาพ ผู้ใช้สามารถคลิกที่ภาพของ TableView และภาพนั้นจะถูกขยายในหน้าต่างที่ใหญ่กว่าอีกหน้าต่างประเภท QLabel
ฉันสามารถทำให้สิ่งนี้เป็นไปได้ด้วยการคลิกเมาส์บน TableView แต่ฉันต้องการเปิดใช้งานสำหรับปุ่มบนคีย์บอร์ด "ขึ้น" และ "ลง" สิ่งที่ฉันหมายถึงคือ เมื่อผู้ใช้คลิกที่ภาพใดภาพหนึ่งที่แสดงอยู่ใน TableView หากผู้ใช้เปลี่ยนเป็นภาพอื่นโดยใช้ปุ่มแป้นพิมพ์ "ขึ้น" และ "ลง" ฉันต้องการตรวจจับการกดปุ่มและเชื่อมต่อกับ QLabel
ซึ่งจะขยายภาพที่เลือกไว้โดยเฉพาะ
สิ่งที่ฉันหมายถึงคือฉันต้องการตรวจจับ keypress
บน QTableView
จริงๆ จนถึงตอนนี้ฉันยังทำมันไม่ได้เลย ฉันกำลังติดตั้ง eventfilter
บน viewPort
ของ QTableView
และในฟังก์ชัน eventfilter
ฉันสามารถตรวจพบ mousebuttonpress
ได้ แต่ตรวจไม่พบ keypress
เพื่อแสดงให้คุณเห็นว่าฉันกำลังเข้าใกล้การใช้งานอย่างไร ฉันได้สร้างโปรแกรมง่ายๆ สำหรับการทดสอบด้วย QTableView
และKeypress
ด้านล่างนี้ ฉันได้ให้โค้ดการใช้งาน mainWindow
ของโปรแกรมง่ายๆ นั้นไปแล้ว
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QStandardItemModel>
#include <QDebug>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui->numberTable->viewport()->installEventFilter(this);
connect(ui->FillUp, SIGNAL(clicked()), this, SLOT(fillUp()));
}
void MainWindow::fillUp()
{
model = new QStandardItemModel(3, 3, this);
int counter = 0;
for(int i = 0; i < 3; i++)
{
for(int j = 0; j < 3; j++)
{
QStandardItem * itm = new QStandardItem;
counter++;
itm->setText(QString::number(counter));
model->setItem(i, j, itm);
}
}
ui->numberTable->setModel(model);
ui->numberTable->show();
}
bool MainWindow::eventFilter(QObject * obj, QEvent * ev)
{
if(obj == ui->numberTable->viewport())
{
if(ev->type() == QEvent::MouseButtonPress)
{
qDebug()<<"Mouse button pressed!\n";
}
else if(ev->type() == QEvent::KeyPress)
{
qDebug()<<"Key button pressed\n";
}
}
return QObject::eventFilter(obj, ev);
}
MainWindow::~MainWindow()
{
delete ui;
}
โปรแกรมส่งเอาต์พุต "การกดปุ่มเมาส์" แต่ไม่มีเอาต์พุตสำหรับการกดแป้นพิมพ์ คุณช่วยบอกฉันหน่อยได้ไหมว่าฉันกำลังทำข้อผิดพลาดอยู่ที่ไหน?
ขอบคุณ
ui->numberTable->installEventFilter(this);
? และการลบif(obj == ui->numberTable->viewport())
ก็คุ้มค่าที่จะลอง - person vahancho   schedule 13.08.2014