Results 1 to 9 of 9

Thread: QPushButton Requires Two Clicks (Not Always)

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Aug 2020
    Posts
    5
    Qt products
    Qt5
    Platforms
    MacOS X

    Default 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.jpg


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




    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.jpg

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




    Any ideas?

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,229
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QPushButton Requires Two Clicks (Not Always)

    Here are the only lines of code in my project
    Wow, you have an entire application that runs with only two methods of one line each? No header files, no main.cpp, no MainWindow constructor? Amazing.

    Showing screenshots of Qt Designer in meaningless. All it shows is that you've specified a UI file for your project. The problem is almost certainly not in the layout of your GUI, it is in how you are connecting the signals and slots of the various UI components and what you are doing in those slots.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. #3
    Join Date
    Aug 2020
    Posts
    5
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: QPushButton Requires Two Clicks (Not Always)

    Quote Originally Posted by d_stranz View Post
    Wow, you have an entire application that runs with only two methods of one line each? No header files, no main.cpp, no MainWindow constructor? Amazing.

    Showing screenshots of Qt Designer in meaningless. All it shows is that you've specified a UI file for your project. The problem is almost certainly not in the layout of your GUI, it is in how you are connecting the signals and slots of the various UI components and what you are doing in those slots.
    Whatever comes with a default new project is all that is in the rest of the code. The only thing I did was add a slot for the buttons like I said. Didn't think they would be important but here they are. If this is a default feature that's pretty weird lol.
    It seems that what ever button is in the position of the remove button will be affected. If i swapped the add with remove, the remove button will be the one requiring two clicks after the add button is clicked. My guess about it being the layout is because of this reason. It affects that specific position in the layout.


    Here is the project, I'm curious if it happens to anyone else running it.

    test.zip


    #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

    #include "mainwindow.h"

    #include <QApplication>

    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);
    MainWindow w;
    w.show();
    return a.exec();
    }


    #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");

    }

    <?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="3" column="0">
    <layout class="QVBoxLayout" name="verticalLayout"/>
    </item>
    <item row="0" column="0">
    <widget class="QListWidget" name="listWidget">
    <item>
    <property name="text">
    <string>New Item</string>
    </property>
    </item>
    <item>
    <property name="text">
    <string>New Item</string>
    </property>
    </item>
    <item>
    <property name="text">
    <string>New Item</string>
    </property>
    </item>
    <item>
    <property name="text">
    <string>New Item</string>
    </property>
    </item>
    <item>
    <property name="text">
    <string>New Item</string>
    </property>
    </item>
    <item>
    <property name="text">
    <string>New Item</string>
    </property>
    </item>
    </widget>
    </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 class="QMenuBar" name="menubar">
    <property name="geometry">
    <rect>
    <x>0</x>
    <y>0</y>
    <width>800</width>
    <height>22</height>
    </rect>
    </property>
    </widget>
    <widget class="QStatusBar" name="statusbar"/>
    </widget>
    <resources/>
    <connections/>
    </ui>

    Last edited by TravisLedo; 15th August 2020 at 02:42.

  4. #4
    Join Date
    Aug 2020
    Posts
    5
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: QPushButton Requires Two Clicks (Not Always)

    Not sure why the site is not allowing me to edit my post but I forgot to mention that even if I don't touch the code at all. If I don't add slots to the buttons or anything. It still happens. I can still see the remove button only showing that it's clicked on the second attempt.

  5. #5
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,229
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QPushButton Requires Two Clicks (Not Always)

    For one thing, you aren't doing any error checking at all in your slots. You don't check the value of "currentRow()" and I would bet you real money that the first time you click, it has the value -1 because there is no current item. If you run your code in a debugger and set a breakpoint inside the on_removeItemButton_clicked() slot I am certain that you will see it gets called every time you click the button.

    Because you have slots named "on_<buttonName>_clicked()", Qt's MOC automatically connects those slots to the corresponding buttons' "clicked()" signals. If somewhere in your code you also connect the button signal and your slot manually, then the slot will be called twice for every click. Most developers have learned to not let this automatic connection happen by naming their slots to something other than "on_<buttonName>_clicked()" because at some point everyone makes the mistake of having double connections which lead to all sorts of behavior that is hard to debug.

    Your call to takeItem() creates a memory leak. takeItem() returns a pointer to the QListWidgetItem instance in that row, and you are responsible for deleting it.

    Your UI file has an empty vertical layout at the bottom. I am guessing that what you really want is to put your whole central widget inside this layout.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  6. #6
    Join Date
    Aug 2020
    Posts
    5
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: QPushButton Requires Two Clicks (Not Always)

    Quote Originally Posted by d_stranz View Post
    For one thing, you aren't doing any error checking at all in your slots. You don't check the value of "currentRow()" and I would bet you real money that the first time you click, it has the value -1 because there is no current item. If you run your code in a debugger and set a breakpoint inside the on_removeItemButton_clicked() slot I am certain that you will see it gets called every time you click the button.

    Because you have slots named "on_<buttonName>_clicked()", Qt's MOC automatically connects those slots to the corresponding buttons' "clicked()" signals. If somewhere in your code you also connect the button signal and your slot manually, then the slot will be called twice for every click. Most developers have learned to not let this automatic connection happen by naming their slots to something other than "on_<buttonName>_clicked()" because at some point everyone makes the mistake of having double connections which lead to all sorts of behavior that is hard to debug.

    Your call to takeItem() creates a memory leak. takeItem() returns a pointer to the QListWidgetItem instance in that row, and you are responsible for deleting it.

    Your UI file has an empty vertical layout at the bottom. I am guessing that what you really want is to put your whole central widget inside this layout.


    Your responses are about the slots. It happens if I don't touch the code at all so I am sure the slots are not causing the issue. The central widget cant be moved, thats the main grid. I took out the extra vertical layout but it still happens. The only difference now is it happens after clicking the button next to the remove button and then back to the add. No slots in any of them. They are just blank buttons. I am just watching the color of the button when clicking. And just to get your mind off the slots i commented them out too.




    What else can it be? It happens more and more, the more widgets and layouts I start adding. But this example is pretty darn simple, this shouldn't be happening at all.




    EDIT: To make it even more simple. I took out the listview widget. So now it's just literally 3 buttons inside 2 different horizontal layouts. It still happens.







    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include <QMainWindow>
    5.  
    6. QT_BEGIN_NAMESPACE
    7. namespace Ui { class MainWindow; }
    8. QT_END_NAMESPACE
    9.  
    10. class MainWindow : public QMainWindow
    11. {
    12. Q_OBJECT
    13.  
    14. public:
    15. MainWindow(QWidget *parent = nullptr);
    16. ~MainWindow();
    17.  
    18. private slots:
    19.  
    20. //void on_removeItemButton_clicked();
    21.  
    22. //void on_addItemButton_clicked();
    23.  
    24. private:
    25. Ui::MainWindow *ui;
    26. };
    27. #endif // MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3.  
    4. MainWindow::MainWindow(QWidget *parent)
    5. : QMainWindow(parent)
    6. , ui(new Ui::MainWindow)
    7. {
    8. ui->setupUi(this);
    9. }
    10.  
    11. MainWindow::~MainWindow()
    12. {
    13. delete ui;
    14. }
    15.  
    16.  
    17. //void MainWindow::on_removeItemButton_clicked()
    18. //{
    19. // ui->listWidget->takeItem(ui->listWidget->currentRow());
    20.  
    21. //}
    22.  
    23. //void MainWindow::on_addItemButton_clicked()
    24. //{
    25. // ui->listWidget->addItem("test");
    26.  
    27. //}
    To copy to clipboard, switch view to plain text mode 



    Qt Code:
    1. #include "mainwindow.h"
    2.  
    3. #include <QApplication>
    4.  
    5. int main(int argc, char *argv[])
    6. {
    7. QApplication a(argc, argv);
    8. MainWindow w;
    9. w.show();
    10. return a.exec();
    11. }
    To copy to clipboard, switch view to plain text mode 




    Qt Code:
    1. <?xml version="1.0" encoding="UTF-8"?>
    2. <ui version="4.0">
    3. <class>MainWindow</class>
    4. <widget class="QMainWindow" name="MainWindow">
    5. <property name="geometry">
    6. <rect>
    7. <x>0</x>
    8. <y>0</y>
    9. <width>800</width>
    10. <height>600</height>
    11. </rect>
    12. </property>
    13. <property name="windowTitle">
    14. <string>MainWindow</string>
    15. </property>
    16. <widget class="QWidget" name="centralwidget">
    17. <layout class="QGridLayout" name="gridLayout">
    18. <item row="1" column="0">
    19. <layout class="QHBoxLayout" name="horizontalLayout">
    20. <item>
    21. <widget class="QPushButton" name="addItemButton">
    22. <property name="text">
    23. <string>Add Item</string>
    24. </property>
    25. </widget>
    26. </item>
    27. </layout>
    28. </item>
    29. <item row="2" column="0">
    30. <layout class="QHBoxLayout" name="horizontalLayout_2">
    31. <item>
    32. <widget class="QPushButton" name="removeItemButton">
    33. <property name="text">
    34. <string>Remove Item</string>
    35. </property>
    36. </widget>
    37. </item>
    38. <item>
    39. <widget class="QPushButton" name="pushButton">
    40. <property name="text">
    41. <string>PushButton</string>
    42. </property>
    43. </widget>
    44. </item>
    45. </layout>
    46. </item>
    47. </layout>
    48. </widget>
    49. </widget>
    50. <resources/>
    51. <connections/>
    52. </ui>
    To copy to clipboard, switch view to plain text mode 




    Updated project file
    test.zip
    Last edited by TravisLedo; 16th August 2020 at 19:00.

  7. #7
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,229
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QPushButton Requires Two Clicks (Not Always)

    If you have commented out all of the slots that are connected to your buttons, then how can you tell that it takes two clicks on any button to get a response? What exactly are you doing to determine that?
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  8. #8
    Join Date
    Aug 2020
    Posts
    5
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: QPushButton Requires Two Clicks (Not Always)

    Quote Originally Posted by d_stranz View Post
    If you have commented out all of the slots that are connected to your buttons, then how can you tell that it takes two clicks on any button to get a response? What exactly are you doing to determine that?
    When you click them they turn blue for a second or until you let go of the mouse. Does not happen on the first click when the problem arises. Also can't be a coincidence that both the click animation and slot are not getting triggered at the same time. It's def the button not receiving the click. I also could just add the slot back with just print statements. Same thing. Does not show click animation or call the function.

  9. #9
    Join Date
    Dec 2008
    Posts
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QPushButton Requires Two Clicks (Not Always)

    Yes, I have this problem too, if i put the btn to layout, i must clicked twitce; if don't use QVBoxLayout it's work

    But, If i set tabWidget is documentMode, It is work.

    my system is OSX 10.15.6, Qt: 5.15

    #include "preferencewidget.h"
    #include "ui_preferencewidget.h"
    #include <QTabWidget>
    #include <QPushButton>
    #include <QDebug>
    #include <QLayout>
    #include <QVBoxLayout>

    PreferenceWidget::PreferenceWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::PreferenceWidget)
    {
    ui->setupUi(this);

    QTabWidget *tab = new QTabWidget(this);

    QWidget *browser_tab = new QWidget;
    QWidget *users_tab = new QWidget;

    tab->addTab(browser_tab, "Browser");
    tab->addTab(users_tab, "Users");

    tab->setGeometry(20, 20, 300, 250);

    QPushButton *btn = new QPushButton("Btn1");
    btn->setGeometry(10, 30, 100, 40);
    btn->setParent(users_tab);

    QVBoxLayout *vl = new QVBoxLayout(users_tab);
    vl->addWidget(btn);
    users_tab->setLayout(vl);


    connect(btn, SIGNAL(clicked()), this, SLOT(clickedAlert()));
    }

    PreferenceWidget::~PreferenceWidget()
    {
    delete ui;
    }


    void PreferenceWidget::clickedAlert()
    {
    qDebug() << "Clicked";
    }
    Last edited by kyosold; 25th August 2020 at 12:04.

Similar Threads

  1. Replies: 3
    Last Post: 3rd November 2017, 00:03
  2. Expanded QComboBox Prevents QPushButton Clicks
    By sean8051 in forum Qt Programming
    Replies: 1
    Last Post: 9th September 2016, 20:04
  3. Replies: 3
    Last Post: 8th August 2016, 14:20
  4. QPushButton,Several unclicked clicks too many
    By thefatladysingsopera in forum Newbie
    Replies: 6
    Last Post: 16th August 2011, 10:51
  5. Replies: 2
    Last Post: 20th December 2010, 18:51

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.