PDA

View Full Version : events don't work



hollowhead
23rd March 2010, 09:59
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
void x11mouseEvent(QMouseEvent *event);
bool returnKey(QKeyEvent* event);
private:
QTableWidget *table;
QEvent *event;


function code



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

nish
23rd March 2010, 10:51
use mousePressEvent() and keyPressEvent()

hollowhead
24th March 2010, 09:53
Thanks MrDeath

This code below works perfectly for keyboard haven't tried mouse yet but I'm sure it will be fine, incidentally how do you formally post a thanks? I cannot work out what to do.




void Spreadsheet::keyPressEvent( QKeyEvent *e )
{
if ( e->key() == Qt::Key_Return )
{
moveDown();
}


else
return;

}

hollowhead
24th March 2010, 10:42
Actually I spoke too soon there is a problem! Pressing return works fine, the problem is pressing any other key doesn't work. To enter anything in the cells you have to double click with a mouse. The F2 key doesn't work either. I tried altering the code to this, but it doesn't work although it compiles and again no run errors.....

Can anyone tell me what's wrong thanks in advance...




void Spreadsheet::keyPressEvent( QKeyEvent *event )
{
if ( event->key() == Qt::Key_Return )

{
moveDown();
}

else
{
QWidget::keyPressEvent(event);
}

}
void QWidget::keyPressEvent(QKeyEvent *event)
{
event->ignore();
}

wysota
24th March 2010, 12:39
Are you sure you are overriding events of proper widgets? What is the point of handling events that are meant for a child widget (the table widget) in its parent (the "Spreadsheet" widget)?

And by the way - you are probably doing the wrong thing in the first place. Middle mouse button already has the paste functionality if you are running on a x11 system so it's just a matter of convincing your table it should actually perform the paste and the return key should also do what you want by itself if you tweak the values of properties of the table widget.

hollowhead
24th March 2010, 17:27
wysota you are right about the X11 mouse issue. There is some functionality without the code I posted although not as much as calc, in calc you copy with the left button and centre click in any cell and then the contents are pasted in. In a tablewidget you have to double (left)click first. Gnumeric has no centre button capabilities. I was hoping to replicate this calc behaviour but can live w/o it. The enter key not moving a cell down is annoying I'm surprised it is not default behaviour for the widget. I wonder if its possible to look for a regular expression for all other keys? Excuse my ignorance still a beginner....

wysota
24th March 2010, 21:39
You are thinking low-level. Don't think in terms of keys, events and buttons. Think in terms of functionality you want to achieve.

You can start by taking a look at QAbstractItemView::closeEditor()

hollowhead
24th March 2010, 22:33
Thanks I think that is beyond me I'll comment out the code and live with the behaviour until my knowledge grows.... :)