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

QPushButton Requires Two Clicks (Not Always)



  • MacOS 10.15.16
    QT version 5.15
    Creator 4.12.4

    Hey everyone,

    I am having a really annoying issue with buttons in my project. I made a very simple project that can replicate the bug.
    What happens is when I click the remove button to remove an item, the next time I click the add button will always require me to click it twice. It does not register the first time.
    I narrowed down the issue but have no idea how to fix it.
    The problem is from layouts I think. The first screenshot shows it working works fine, does not require two clicks. But when I add the new button in the same layout as the remove button, that is when it happens.

    Screen Shot 2020-08-13 at 6.11.22 PM.png

    Screen Shot 2020-08-13 at 6.11.02 PM.png

    Here are the only lines of code in my project

    void MainWindow::on_removeItemButton_clicked()
    {
    ui->listWidget->takeItem(ui->listWidget->currentRow());

    }

    void MainWindow::on_addItemButton_clicked()
    {
    ui->listWidget->addItem("test");

    }

    I noticed that this happens with many things I try to do. Here is an instance where if I put everything inside a Tab Widget, the first time running the app will require two clicks on any of those buttons.
    Clicking on an itemList item however works fine.

    Screen Shot 2020-08-13 at 6.25.35 PM.png

    The issue only applies to buttons. Nothing else is being affected by this.

    Any ideas?



  • @TravisLedo
    This is only a guess:

    ui->listWidget->takeItem(ui->listWidget->currentRow());
    

    You have taken out the "current row". I wonder what has happened to the current row now? I wonder if that is relevant to the next click?

    Certainly, if we ignore the remove and just do two Add Items in a row I would not expect any funny behaviour. Are you claiming that does not work right? If so i would suspect other code somewhere; try to reproduce that in a minimal application.



  • @JonB Hey thanks for responding.
    Here is my update from another forum regarding this issue.

    So what I have done so far is reduce the program to something VERY simple. There is no more listView. There are only 3 buttons inside two horizontal layouts. And this still happens. The only difference is now it happens to the button next to the remove button. When it click that button and try to click the add button after that, it requires two clicks.

    I took out every slot code. They are just blank now and im watching the color change as i click.
    Any ideas?

    <?xml version="1.0" encoding="UTF-8"?>
    <ui version="4.0">
     <class>MainWindow</class>
     <widget class="QMainWindow" name="MainWindow">
      <property name="geometry">
       <rect>
        <x>0</x>
        <y>0</y>
        <width>800</width>
        <height>600</height>
       </rect>
      </property>
      <property name="windowTitle">
       <string>MainWindow</string>
      </property>
      <widget class="QWidget" name="centralwidget">
       <layout class="QGridLayout" name="gridLayout">
        <item row="1" column="0">
         <layout class="QHBoxLayout" name="horizontalLayout">
          <item>
           <widget class="QPushButton" name="addItemButton">
            <property name="text">
             <string>Add Item</string>
            </property>
           </widget>
          </item>
         </layout>
        </item>
        <item row="2" column="0">
         <layout class="QHBoxLayout" name="horizontalLayout_2">
          <item>
           <widget class="QPushButton" name="removeItemButton">
            <property name="text">
             <string>Remove Item</string>
            </property>
           </widget>
          </item>
          <item>
           <widget class="QPushButton" name="pushButton">
            <property name="text">
             <string>PushButton</string>
            </property>
           </widget>
          </item>
         </layout>
        </item>
       </layout>
      </widget>
     </widget>
     <resources/>
     <connections/>
    </ui>
    
    
    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    
    MainWindow::MainWindow(QWidget *parent)
        : QMainWindow(parent)
        , ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    
    
    //void MainWindow::on_removeItemButton_clicked()
    //{
    //    ui->listWidget->takeItem(ui->listWidget->currentRow());
    
    //}
    
    //void MainWindow::on_addItemButton_clicked()
    //{
    //    ui->listWidget->addItem("test");
    
    //}
    
    
    #include "mainwindow.h"
    
    #include <QApplication>
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        MainWindow w;
        w.show();
        return a.exec();
    }
    
    
    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    
    #include <QMainWindow>
    
    QT_BEGIN_NAMESPACE
    namespace Ui { class MainWindow; }
    QT_END_NAMESPACE
    
    class MainWindow : public QMainWindow
    {
        Q_OBJECT
    
    public:
        MainWindow(QWidget *parent = nullptr);
        ~MainWindow();
    
    private slots:
    
        //void on_removeItemButton_clicked();
    
        //void on_addItemButton_clicked();
    
    private:
        Ui::MainWindow *ui;
    };
    #endif // MAINWINDOW_H
    
    

Log in to reply