I tried setting up both a mouse and keyboard event to try to learn a bit more about Qt and C++. The mouse event is to make the centre mouse button available for paste and the keyboard event to mean when the return key is pressed in a tablewidget the cursor moves to the next cell down. Both compile neither work and no errors are given. The mouse would be nice the return key is annoying and hence more important. Where am I going wrong?
Class and function defs
public:// public
void moveDown();
protected: // protected
private:
public:// public
void moveDown();
protected: // protected
void x11mouseEvent(QMouseEvent *event);
bool returnKey(QKeyEvent* event);
private:
QTableWidget *table;
QEvent *event;
To copy to clipboard, switch view to plain text mode
function code
// centre mouse uses an existing paste() function
{
if (event->button()==Qt::MidButton && clipboard->supportsSelection())
{
paste(); // calls paste function for spreadsheet (this function works fine just not with centre mouse)
}
}
// cell down functions
void Spreadsheet::moveDown()
{
int row=currentRow();
row += 1;
int column=currentColumn();
if (row>=RowCount) // check bounds are possible
{
row=0;
}
setCurrentCell(row, column);
}
bool Spreadsheet
::returnKey(QKeyEvent* event
) {
if (event
->type
()==QEvent::KeyPress) {
QKeyEvent *keyEvent
=static_cast<QKeyEvent
*>
(event
);
if ((keyEvent->key()==Qt::Key_Return) && (table->hasFocus()) ) // hasFocus probably not needed doesn't help
{
moveDown();
return true;
}
}
}
// centre mouse uses an existing paste() function
void Spreadsheet::x11mouseEvent(QMouseEvent *event)
{
QClipboard *clipboard = QApplication::clipboard();
if (event->button()==Qt::MidButton && clipboard->supportsSelection())
{
QString text = clipboard->text(QClipboard::Selection);
paste(); // calls paste function for spreadsheet (this function works fine just not with centre mouse)
}
}
// cell down functions
void Spreadsheet::moveDown()
{
int row=currentRow();
row += 1;
int column=currentColumn();
if (row>=RowCount) // check bounds are possible
{
row=0;
}
setCurrentCell(row, column);
}
bool Spreadsheet::returnKey(QKeyEvent* event)
{
if (event->type()==QEvent::KeyPress)
{
QKeyEvent *keyEvent=static_cast<QKeyEvent *>(event);
if ((keyEvent->key()==Qt::Key_Return) && (table->hasFocus()) ) // hasFocus probably not needed doesn't help
{
moveDown();
return true;
}
}
return QWidget::event(event);
}
To copy to clipboard, switch view to plain text mode
Bookmarks