PDA

View Full Version : Mouse event problem



cheyanne
24th December 2011, 08:32
how can i get the coordinates feedback if i click the left button on my qgraphicscene.


void MainWindow::mousePressEvent(QMouseEvent *event)
{
if(event->button() == Qt::LeftButton )
{
//im not sure what code i should put in here
}
}

Lykurg
24th December 2011, 09:09
What's about QMouseEvent::globalPos() and QMouseEvent::pos()?

cheyanne
24th December 2011, 11:39
im able to get the coordinates from the mainwindow, which mean (0,0) is at the top left of the mainwindow, how do i set (0,0) at the top left of my qgraphicsview?

Lykurg
24th December 2011, 11:42
then see QWidget::mapTo() or QWidget::mapFrom().

cheyanne
24th December 2011, 15:08
i cant get it, you have any source codes as reference ?? how about you tell me if i can enable mouse event only in my qgraphicsview?

Lykurg
24th December 2011, 15:25
Only because today it is Christmas eve! (normally it shouldn't be a problem for you, to write a small demo app, where you can play around.)

#include <QtGui>

class Main : public QWidget
{
Q_OBJECT
public:
Main(QWidget *parent = 0) : QWidget(parent) {
m_green = new QWidget(this);
QPalette p = m_green->palette();
p.setColor(QPalette::Window, Qt::green);
m_green->setPalette(p);
m_green->setAutoFillBackground(true);
m_green->setMinimumSize(50, 50);

QHBoxLayout *l = new QHBoxLayout;
l->addWidget(m_green);
l->setContentsMargins(100, 100, 100, 100);
setLayout(l);
}
virtual ~Main() {}
protected:
void mousePressEvent(QMouseEvent *e) {
qWarning() << Q_FUNC_INFO;
qWarning() << " pos:" << e->pos();
qWarning() << " gpos:" << e->globalPos();
qWarning() << "child:"
<< m_green->mapFromParent(e->pos())
<< m_green->mapFromGlobal(e->globalPos())
<< m_green->mapFrom(this, e->pos());
}
private:
QWidget *m_green;
};


int main(int argc, char *argv[])
{
QApplication a(argc, argv);

Main w;
w.show();

return a.exec();
}

#include "main.moc"

cheyanne
24th December 2011, 15:48
thanks:) how about mouseevent only in qgraphicsview? not anywhere else. Can this be done ?

Lykurg
24th December 2011, 16:05
yes, but you will find out yourself... By reading the documentation.

cheyanne
27th December 2011, 07:13
Do I use mouse area?