duplicate objects issue in Qt Creator (C++ programming)
-
Hello to all.
I have a "duplicate objects" issue in Qt Creator (version 6.5.1) on Windows 10 Pro. I'm using MSVC2019 64-bit compiler:
The application includes a "WidgetLine.ui" window on which there is a "button_add_line" button, intended to allow the creation of "Line" type objects. Here is the part of the code concerned, in the "WidgetLine" class:WidgetLine::WidgetLine(QWidget* parent):
QWidget{parent}
,ui(new Ui::WidgetLine)
//...
{
ui->setupUi(this);
//...
connect(ui->button_add_line,&QPushButton::clicked,this,&WidgetLine::on_button_add_line_clicked);
//...
}void WidgetLine::on_button_add_line_clicked()
{
//..._line= new Line();//_line is a private member in "WidgetLine.h": Line* _line=nullptr;
//...
}Problem observed: two instances of "Line" are created each time the button is clicked. To fix it, I had to "tinker" by adding in the slot the following instructions, just before the instantiation:
static int countClicks=0;
if ((++countClicks % 2)==0)
return;This "fix" allows me to create the instances without duplication, but I would like to be able to trace the real source of the problem and fix it in a more elegant way.
Everything seems to indicate that each time the button "button_add_line" is clicked, the signal "clicked" is emitted twice! Could someone enlighten me on what is wrong?
I tried the old fashioned connection mode:
connect(ui->button_add_line,SIGNAL(clicked()),this,SLOT(on_button_add_line_clicked()));
but, it behaves exactly the same way!
In advance, thank you for your answers. -
Hello to all.
I have a "duplicate objects" issue in Qt Creator (version 6.5.1) on Windows 10 Pro. I'm using MSVC2019 64-bit compiler:
The application includes a "WidgetLine.ui" window on which there is a "button_add_line" button, intended to allow the creation of "Line" type objects. Here is the part of the code concerned, in the "WidgetLine" class:WidgetLine::WidgetLine(QWidget* parent):
QWidget{parent}
,ui(new Ui::WidgetLine)
//...
{
ui->setupUi(this);
//...
connect(ui->button_add_line,&QPushButton::clicked,this,&WidgetLine::on_button_add_line_clicked);
//...
}void WidgetLine::on_button_add_line_clicked()
{
//..._line= new Line();//_line is a private member in "WidgetLine.h": Line* _line=nullptr;
//...
}Problem observed: two instances of "Line" are created each time the button is clicked. To fix it, I had to "tinker" by adding in the slot the following instructions, just before the instantiation:
static int countClicks=0;
if ((++countClicks % 2)==0)
return;This "fix" allows me to create the instances without duplication, but I would like to be able to trace the real source of the problem and fix it in a more elegant way.
Everything seems to indicate that each time the button "button_add_line" is clicked, the signal "clicked" is emitted twice! Could someone enlighten me on what is wrong?
I tried the old fashioned connection mode:
connect(ui->button_add_line,SIGNAL(clicked()),this,SLOT(on_button_add_line_clicked()));
but, it behaves exactly the same way!
In advance, thank you for your answers.@Bineere said in duplicate objects issue in Qt Creator (C++ programming):
each time the button "button_add_line" is clicked, the signal "clicked" is emitted twice
This implies you have done the
connect()
twice.I bet that in addition to your explicit
connect()
you are using the Designer's default of auto-connect. That is because you have named your sloton_button_add_line_clicked
, it's to do with that, and your button is named something likebutton_add_line
.Either change the name, or switch off the auto-connecting.
-
-
-
C Christian Ehrlicher restored this topic on