Trolltech Home | Qt-interest Home | Recent Threads | All Threads | Author | Date
All threads index page 2

Qt-interest Archive, April 2007
sender() not working in Qt


Message 1 in thread

Hello,

I have created a Qt application (using Qt 3.3.2 on RedHat Linux) that need to run on a touchscreen, where mouse is not used. By using a protocol, we are reading x & y co-ordinates from serial port, depending on finger touch, we are setting the mouse cursor to the appropriate position using Qt calls. But the problem is after placing the mouse cursor on top of a button, we cannot fake the mouse click, by creating the QMouseEvent object and using sendEvent(). The code used is as follows:

Please find below the code snippet:

void MouseForm::mouseMoveEvent(QMouseEvent *e)
{
   int x=e->globalPos().x();
  int y=e->globalPos().y();
QWidget *w=QApplication::widgetAt(e->globalPos().x(),globalPos().y(),TRUE);

if(w)
{
    QPushButton *pb=::qt_cast<QPushButton *>(w);

    if(pb)
    {
        QMouseEvent
ev1(QEvent::MouseButtonPress,QPoint(x,y),Qt::LeftButton,Qt::LeftButton);
        QApplication::sendEvent(pb,&ev1);
        QMouseEvent
ev1(QEvent::MouseButtonRelease,QPoint(x,y),Qt::LeftButton,Qt::LeftButton);
        QApplication::sendEvent(pb,&ev1);
   }
}
}

void MouseForm::buttonClicked()     // Defined as a SLOT in base class
{
        QPushButton *rec=::qt_cast<QPushButton*>(sender());  //This does not work as rec=0 always
        if(rec)
                label->setText("Button clicked - " + rec->text());
}

I am creating the QMouseEvent objects for button press and release events as I need to fake the mouse click when the mouse cursor is positioned on top of the button.

Does Qt not support any calls used to explicitly generate mouse click events? If it does not, then this is a handicap, as all other languages like Java, VC++ and even JavaScript support this easily.

Can anybody help me to solve this problem?

Pankaj.

Message 2 in thread

You can simulate events using sendEvent as you do. However, you need to know
what the widget expects. For instance, your coordinate are wrong. You send
global coordinates to the button as local coordinates. Use mapFromGlobal to
get local coordinates, and make sure that they are in the button anyway.
I.e. if you have a rounded button like on Windows XP, then using coordinate
0,0 might not trigger a clicked signal.

In order to simulate events you have to know a lot about your receiver
widget, it's look, it's feel and other implementation details.

Volker


"Pankaj" <pankaj@xxxxxxxxxxxxx> wrote in message 
news:ev4lkk$h0a$1@xxxxxxxxxxxxxxxxxxxxx
Hello,

I have created a Qt application (using Qt 3.3.2 on RedHat Linux) that need 
to run on a touchscreen, where mouse is not used. By using a protocol, we 
are reading x & y co-ordinates from serial port, depending on finger touch, 
we are setting the mouse cursor to the appropriate position using Qt calls. 
But the problem is after placing the mouse cursor on top of a button, we 
cannot fake the mouse click, by creating the QMouseEvent object and using 
sendEvent(). The code used is as follows:

Please find below the code snippet:

void MouseForm::mouseMoveEvent(QMouseEvent *e)
{
   int x=e->globalPos().x();
  int y=e->globalPos().y();
QWidget *w=QApplication::widgetAt(e->globalPos().x(),globalPos().y(),TRUE);

if(w)
{
    QPushButton *pb=::qt_cast<QPushButton *>(w);

    if(pb)
    {
        QMouseEvent
ev1(QEvent::MouseButtonPress,QPoint(x,y),Qt::LeftButton,Qt::LeftButton);
        QApplication::sendEvent(pb,&ev1);
        QMouseEvent
ev1(QEvent::MouseButtonRelease,QPoint(x,y),Qt::LeftButton,Qt::LeftButton);
        QApplication::sendEvent(pb,&ev1);
   }
}
}

void MouseForm::buttonClicked()     // Defined as a SLOT in base class
{
        QPushButton *rec=::qt_cast<QPushButton*>(sender());  //This does not 
work as rec=0 always
        if(rec)
                label->setText("Button clicked - " + rec->text());
}

I am creating the QMouseEvent objects for button press and release events as 
I need to fake the mouse click when the mouse cursor is positioned on top of 
the button.

Does Qt not support any calls used to explicitly generate mouse click 
events? If it does not, then this is a handicap, as all other languages like 
Java, VC++ and even JavaScript support this easily.

Can anybody help me to solve this problem?

Pankaj. 


--
 [ signature omitted ]