Originally Posted by
d_stranz
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.
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
{
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
#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
To copy to clipboard, switch view to plain text mode
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow
::MainWindow(QWidget *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 "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");
//}
To copy to clipboard, switch view to plain text mode
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
MainWindow w;
w.show();
return a.exec();
}
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
To copy to clipboard, switch view to plain text mode
<?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>
<?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>
To copy to clipboard, switch view to plain text mode
Updated project file
test.zip
Bookmarks