Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. duplicate objects issue in Qt Creator (C++ programming)
Forum Updated to NodeBB v4.3 + New Features

duplicate objects issue in Qt Creator (C++ programming)

Scheduled Pinned Locked Moved Solved General and Desktop
2 Posts 2 Posters 392 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    Bineere
    wrote on last edited by Bineere
    #1

    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.

    JonBJ 1 Reply Last reply
    0
    • B Bineere

      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.

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @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 slot on_button_add_line_clicked, it's to do with that, and your button is named something like button_add_line.

      Either change the name, or switch off the auto-connecting.

      1 Reply Last reply
      1
      • B Bineere deleted this topic on
      • B Bineere has marked this topic as solved on
      • Christian EhrlicherC Christian Ehrlicher restored this topic on

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved