Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Is there a way to check next events?



  • Hello,

    I would like to check which event will be after QEvent::FocusIn in my application.

    I am now in FocusIn function and I know that will be other events after that ( for example mousePressEvent ). How to check it?

    I think about:

    qDebug()<<eventsQueque.nextEvent();


  • Lifetime Qt Champion

    Hi
    I dont think you can check that.

    Can i ask what you need it for ?



  • @mrjj A few days ago I ask about: why QT doesn't see all clicks on the QPushButton ( if I click on them I show a popup window ).

    Please check this code:

    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    
    #include <QMainWindow>
    #include <QPushButton>
    
    QT_BEGIN_NAMESPACE
    namespace Ui { class MainWindow; }
    QT_END_NAMESPACE
    
    class MainWindow : public QMainWindow
    {
        Q_OBJECT
    
    public:
        MainWindow(QWidget *parent = nullptr);
        ~MainWindow();
    
    public slots:
        void clickedSlot();
    private:
        Ui::MainWindow *ui;
        QPushButton *button;
        QWidget *widget;
    };
    #endif // MAINWINDOW_H
    
    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include <QDebug>
    
    MainWindow::MainWindow(QWidget *parent)
        : QMainWindow(parent)
        , ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
        button = new QPushButton(this);
        button->setGeometry(100,100,100,100);
        connect(button, SIGNAL(clicked()), this, SLOT(clickedSlot()));
        widget = new QWidget;
        widget->setWindowFlag(Qt::Popup);
        widget->setFixedSize(300,300);
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    
    void MainWindow::clickedSlot()
    {
        static int click=1;
        qDebug()<<click;
        if(click%2==1)
        {
            widget->show();
        }
        else
        {
            widget->hide();
        }
        click++;
    }
    
    

    This code above is very simple - I only add pushButton and widget. Now please click 2 times fast in a "button". In a qDebug() I get only "1". QWidget should be hide and it is hide. But this hide is because of popup. Now I would like click next time. Now widget should be visible ( this is the third click ), but it isn't ( because QT thinks that is my second clicked on button ).

    And that queue with events could be helpful for me ( this is only my try - if you know something better to achieve what I want ( the click%2==1 show, click%2==0 hide, but when QT see all clicks ) - please write :) )



  • @TomNow99 said in Is there a way to check next events?:

    I would like to check which event will be after QEvent::FocusIn in my application.

    Only if you can see into the future ;-)

    You do not have access to the Qt event queue. If you really wanted to do something like this, you'd have to not act on current event, wait till the next event arrived and act on it there. Doubtless with a timeout.



  • @TomNow99 said in Is there a way to check next events?:

    void MainWindow::clickedSlot()
    {
    static int click=1;
    qDebug()<<click;
    if(click%2==1)
    {
    widget->show();
    }
    else
    {
    widget->hide();
    }
    click++;
    }

    Instead of counting consecutive clicks, you could check, whether your dialog is shown already or not...

    void MainWindow::clickedSlot()
    {
        if( !widget->isVisible() )
        {
             widget->show();
        }
        else{
            widget->hide();
        }
    }
    


  • @Pl45m4 Of course I tried your solution a few days ago. Please look: when the widget has set flag Qt::Popup and when it is visible, when I clicked pushButton with your code I always get widget->show(). Why? Popup is before clicked() signal, so popup hide widget I go to clicked slot and widget is hide, so I show it. Next click the same. So my every click is widget->show().



  • @TomNow99 said in Is there a way to check next events?:

    when I clicked pushButton with your code I always get widget->show()

    Yeah, that's how popups are supposed to work.

    But why do you want to hide your popup widget especially with this button, when your popup widget will hide on any click anyway? Why do you need Qt::Popup?



  • @Pl45m4 I try create something like comboBox. This comboBox has 2 parts: pushButton and widget. When I clicked on pushButton I would like to show or hide widget. User can click outside this button or click on it, so I have to get part "show" and "hide" in clicked slot.


  • Lifetime Qt Champion


Log in to reply