RomaValcer RomaValcer - 5 months ago 49
C++ Question

Qt delaying processing of mousePressEvent

I have a custom button that can be right clicked in addition to left clicking. It works like this:

private slots:
void mousePressEvent(QMouseEvent *e) {
if(e->button() == Qt::RightButton) {
std::cout << "kek2";
emit rightClicked();

For some weird reason left click works fine, and right clicks are not processed (judging by lack of kek2 outputs) until I do a left click, upon which all right clicks are processed at once. Why?


I had a look at it and implemented an equivalent Button and changed your code to this:

void QRightClickButton::mousePressEvent(QMouseEvent *e)
    if(e->button() == Qt::RightButton) {
                    std::cout << "kek2" << std::endl; //the << std::endl; //made it work good again
                    emit rightClicked();
    } else if(e->button() == Qt::LeftButton) {
     //this else if block is just a fast implementation of the leftclick
                    std::cout << "kek3" << std::endl;


Try it yourself both log messages ("kek2" as well as "kek3") appear instantly. Without the << std::endl; it is still buggy!

EDIT: I decided to add 2 more ways to do it!

  1. You can also use it without std::endl;

    std::cout << message; //prints message

    std::cout.flush(); //flushes the output so it will be visible as well

For example you can create your own class or your own function.

  1. Use qDebug(). qDebug automatically flushes the stream so you don't have to write extra code as well as it belongs to Qt self. That means you're not going to mix 2 libraries.

Use it that way:

qDebug() << message;