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. Classes communication / signal slot generates duplicate connection error
QtWS25 Last Chance

Classes communication / signal slot generates duplicate connection error

Scheduled Pinned Locked Moved General and Desktop
29 Posts 4 Posters 6.4k 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.
  • A Offline
    A Offline
    angelicaP
    wrote on last edited by
    #1

    hi,

    a continuation of my previous post ("public slots in mainWindow":http://qt-project.org/forums/viewthread/43489/)

    in mainwindow.cpp I declare a database connection:

    @MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    ui->setupUi(this);
    QMainWindow::showMaximized();
    {
    QSqlDatabase connect1 = QSqlDatabase::addDatabase("QPSQL", "connect1");
    connect1.setHostName("localhost");
    connect1.setDatabaseName("dbname");
    connect1.setUserName("user1");
    connect1.setPassword("test1");
    connect1.open();
    connect1.close();
    }}@

    when I connect a new widget using the addNewTab slot declared here("public slots in mainWindow":http://qt-project.org/forums/viewthread/43489/),

    @void formSelect::on_pushButtonCreate_clicked()
    {
    formCreate *fC = new formCreate;
    MainWindow *mW = new MainWindow;
    QString title = "Create";

    connect(ui->pushButtonCreate, SIGNAL(clicked()), mW, SLOT(addNewTab(QWidget *fC ,QString &title)));
    

    }@

    I receive this error: "QSqlDatabasePrivate::addDatabase: duplicate connection name 'connect1', old connection removed."

    any Idea what I'm doing wrong?

    I tried to "disconnect(ui->pushButtonCreate, 0, 0, 0);" before calling the connect function, but no positive result.

    thank you

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      each time you call on_pushButtonCreate_clicked you create a new MainWindow which in turn will create a new database connection.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • IamSumitI Offline
        IamSumitI Offline
        IamSumit
        wrote on last edited by
        #3

        Hii.

        [quote author="angelicaP" date="1402002250"]

        @void formSelect::on_pushButtonCreate_clicked()
        {
        formCreate *fC = new formCreate;
        MainWindow *mW = new MainWindow;
        QString title = "Create";

        connect(ui->pushButtonCreate, SIGNAL(clicked()), mW, SLOT(addNewTab(QWidget *fC ,QString &title)));
        

        }@

        [/quote]
        Problem :
        in line4 when you do click each time it creates a new object which forces to remove old database connection and makes a replica of a new connection for new object.

        Sol:
        You need to create one object that holds one connection at a time.
        Code snippet:
        in class formSelect constructor
        pass the reference of MainWindow class object at one time something like this

        formSelect(Mainwindow *pObj)
        {
        pmainWindowRef=pObj; //Where pmainWindow is the MainWindow pointer variable.
        }

        hope it helps.

        Be Cute

        1 Reply Last reply
        0
        • A Offline
          A Offline
          angelicaP
          wrote on last edited by
          #4

          SGaist, thank you. that makes sense.

          lamSumit, thank you, too. do I have to change the parent of the formSelect?

          1 Reply Last reply
          0
          • IamSumitI Offline
            IamSumitI Offline
            IamSumit
            wrote on last edited by
            #5

            [quote author="angelicaP" date="1402259352"]SGaist, thank you. that makes sense.
            do I have to change the parent of the formSelect?[/quote]

            No you don't need this,You can achieve it the solution that i have given to you.

            Be Cute

            1 Reply Last reply
            0
            • A Offline
              A Offline
              angelicaP
              wrote on last edited by
              #6

              I don't understand what are you referring to when you use:

              pmainWindowRef

              pObj, is clear is the parent Widget, which is a pointer of the MainWindow, but pmainWindowRef ???

              many thanks for your support.

              1 Reply Last reply
              0
              • IamSumitI Offline
                IamSumitI Offline
                IamSumit
                wrote on last edited by
                #7

                ok let me clear..
                You have 2 classes MainWindow and FromSelect.
                you have to create an instance of class MainWindow to class FromSelect

                • in FromSelect .h file
                  @
                  #include "MainWindow"
                  class MainWindow;
                  class FromSelect:public QWidget
                  {
                  Q_OBJECT
                  public:
                  FromSelect(MainWindow *pObj,QWidget *parent=0) ;
                  ~FromSelect() ;
                  private slots:
                  void on_pushButtonCreate_clicked();
                  private:
                  MainWindow *pmainWindowRef;
                  };
                  @

                • in FromSelect.cpp file
                  @
                  FromSelect(MainWindow *pObj,QWidget *parent) :QWidget(parent)
                  {
                  pmainWindowRef=pObj; //pmainWindowRef holds the reference of Mainwindow
                  }
                  void FromSelect::on_pushButtonCreate_clicked()
                  {
                  formCreate *fC = new formCreate;
                  QString title = "Create";
                  connect(ui->pushButtonCreate, SIGNAL(clicked()), pmainWindowRef,SLOT(addNewTab(QWidget *fC ,QString &title)));
                  }
                  @

                hope it helps :)

                Be Cute

                1 Reply Last reply
                0
                • A Offline
                  A Offline
                  angelicaP
                  wrote on last edited by
                  #8

                  @MainWindow *pmainWindowRef;@

                  gives me the following error:
                  "MainWindow does not name a type;",
                  with
                  @#include "mainwindow.h"@
                  and with
                  @class MainWindow@
                  included

                  my question is: why I don't receive the same error for other classes, like
                  @formCreate *fC;@

                  any idea?

                  1 Reply Last reply
                  0
                  • SGaistS Offline
                    SGaistS Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    Before going any further, what do you want to achieve with FromSelect ?

                    Interested in AI ? www.idiap.ch
                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                    1 Reply Last reply
                    0
                    • A Offline
                      A Offline
                      angelicaP
                      wrote on last edited by
                      #10

                      FormSelect is only a form where the pushButtonCreate is located, and the idea is if I emit the _clicked() _ signal of this button, the formCreate should be opened in MainWindow, into a new tab of the already existing tabWidget.

                      formSelect is only a transitional phase.

                      thank you.

                      1 Reply Last reply
                      0
                      • SGaistS Offline
                        SGaistS Offline
                        SGaist
                        Lifetime Qt Champion
                        wrote on last edited by
                        #11

                        Then you should only forward the clicked signal from that widget and do the connection directly in MainWindow.

                        Interested in AI ? www.idiap.ch
                        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                        1 Reply Last reply
                        0
                        • A Offline
                          A Offline
                          angelicaP
                          wrote on last edited by
                          #12

                          hi,

                          I edited the subject, because the post has changed from the original one.

                          I'm sure this issue is not rocket science, but for me is a no go.
                          I tried to reduce my examples to minimum, only to establish this connection. I tried to create the connection in MainWindow as you mentioned. here are is my complete code:

                          @// dialog2.cpp

                          #include "dialog2.h"
                          #include "ui_dialog2.h"

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

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

                          // dialog2.h

                          #ifndef DIALOG2_H
                          #define DIALOG2_H

                          #include <QDialog>

                          namespace Ui {
                          class Dialog2;
                          }

                          class Dialog2 : public QDialog
                          {
                          Q_OBJECT

                          public:
                          explicit Dialog2(QWidget *parent = 0);
                          ~Dialog2();

                          signals:
                          void on_pushButton_clicked();

                          private:
                          Ui::Dialog2 *ui;
                          };

                          #endif // DIALOG2_H

                          //dialog1.cpp
                          #include "dialog1.h"
                          #include "ui_dialog1.h"

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

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

                          //dialog1.h
                          #ifndef DIALOG1_H
                          #define DIALOG1_H

                          #include <QDialog>

                          namespace Ui {
                          class Dialog1;
                          }

                          class Dialog1 : public QDialog
                          {
                          Q_OBJECT

                          public:
                          explicit Dialog1(QWidget *parent = 0);
                          ~Dialog1();

                          private:
                          Ui::Dialog1 *ui;
                          };

                          #endif // DIALOG1_H

                          //mainwindow.h
                          #ifndef MAINWINDOW_H
                          #define MAINWINDOW_H

                          #include <QMainWindow>
                          #include <QTabWidget>

                          namespace Ui {
                          class MainWindow;
                          }

                          class MainWindow : public QMainWindow
                          {
                          Q_OBJECT

                          public:
                          explicit MainWindow(QWidget *parent = 0);
                          ~MainWindow();

                          private:
                          Ui::MainWindow *ui;

                          private slots:
                          void addNewTab(QWidget *page, QString &title)
                          {
                          QTabWidget *newTab = new QTabWidget;
                          setCentralWidget(newTab);
                          newTab->addTab(page, title);

                          }
                          

                          };

                          #endif // MAINWINDOW_H

                          //mainwindow.cpp
                          #include "mainwindow.h"
                          #include "ui_mainwindow.h"
                          #include "dialog1.h"
                          #include "dialog2.h"

                          MainWindow::MainWindow(QWidget *parent) :
                          QMainWindow(parent),
                          ui(new Ui::MainWindow)
                          {
                          ui->setupUi(this);
                          Dialog1 *d1 = new Dialog1;
                          Dialog2 *d2 = new Dialog2;
                          QString title = "title test1";

                          connect(d2, SIGNAL(on_pushButton_clicked()), this, SLOT(addNewTab(QWidget* , QString&)));
                          

                          }

                          MainWindow::~MainWindow()
                          {
                          delete ui;
                          }@

                          this give me the following error:
                          "QObject::connect: Incompatible sender/receiver arguments
                          Dialog2::on_pushButton_clicked() --> MainWindow::addNewTab(QWidget*,QString&)"

                          because the signal is not the same type as the slot, "forcing the socket". so my existential question is, how to connect this pushButton with the slot "addNewTab"; desired action is to understand the communication between these classes when a widget should be opened in a tabWidget in the MainWindow.

                          thank you for your support.

                          1 Reply Last reply
                          0
                          • SGaistS Offline
                            SGaistS Offline
                            SGaist
                            Lifetime Qt Champion
                            wrote on last edited by
                            #13

                            You can't establish that connection, a slot must have either the same list of arguments as the signal or less but still matching from left to right.

                            Interested in AI ? www.idiap.ch
                            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                            1 Reply Last reply
                            0
                            • A Offline
                              A Offline
                              angelicaP
                              wrote on last edited by
                              #14

                              is there any other solution available besides connect (signal/slot) to make this pushButton open a dialog form?

                              1 Reply Last reply
                              0
                              • A Offline
                                A Offline
                                angelicaP
                                wrote on last edited by
                                #15

                                I don't understand, there should be a way to connect this pushButton. maybe to connect it to an intermediate slot? any idea? thank you.

                                1 Reply Last reply
                                0
                                • A Offline
                                  A Offline
                                  andreyc
                                  wrote on last edited by
                                  #16

                                  In the implementation of addNewTab you replace a central widget of a MainWindow. It means that a QTabWidget will have only one tab always.
                                  @
                                  void addNewTab(QWidget *page, QString &title)
                                  {
                                  QTabWidget *newTab = new QTabWidget;
                                  setCentralWidget(newTab);
                                  newTab->addTab(page, title);
                                  }
                                  @

                                  Question: How do you provide page and title for *addNewTab(QWidget page, QString &title) ?

                                  1 Reply Last reply
                                  0
                                  • A Offline
                                    A Offline
                                    angelicaP
                                    wrote on last edited by
                                    #17

                                    bq. In the implementation of addNewTab you replace a central widget of a MainWindow. It means that a QTabWidget will have only one tab always.

                                    I'm confused. this is what I was doing before. please check the line 115 in my above code.

                                    bq. Question: How do you provide page and title for addNewTab(QWidget *page, QString &title) ?

                                    via line 138, but is not working because the signal and the slot doesn't have the same type.

                                    1 Reply Last reply
                                    0
                                    • A Offline
                                      A Offline
                                      andreyc
                                      wrote on last edited by
                                      #18

                                      [quote author="angelicaP" date="1402512166"][quote]In the implementation of addNewTab you replace a central widget of a MainWindow. It means that a QTabWidget will have only one tab always.[/quote] I'm confused. this is what I was doing before. please check the line 115 in my above code.[/quote]
                                      Right, you were replacing centralWidget on each call to addNewTab().
                                      I don't think that it is what you wanted.

                                      [quote author="angelicaP" date="1402512166"][quote]Question: How do you provide page and title for addNewTab(QWidget *page, QString &title) ?[/quote]
                                      via line 138, but is not working because the signal and the slot doesn't have the same type.[/quote]
                                      No, in the line 138 you are trying to set a connection between some signal and some slot. In the slot you expect to get page and title.
                                      My question is where do you get those page and title initially?
                                      Does it come from the form that you are trying to create or do you read it from a file?
                                      BTW you need to create page somewhere.

                                      1 Reply Last reply
                                      0
                                      • A Offline
                                        A Offline
                                        angelicaP
                                        wrote on last edited by
                                        #19

                                        bq. Right, you were replacing centralWidget on each call to addNewTab().
                                        I don’t think that it is what you wanted.

                                        I see now. that means if I remove the setCentralWidget, I need to make sure it's created somewhere else.

                                        bq. No, in the line 138 you are trying to set a connection between some signal and some slot. In the slot you expect to get page and title.
                                        My question is where do you get those page and title initially?
                                        Does it come from the form that you are trying to create or do you read it from a file?
                                        BTW you need to create page somewhere.

                                        my initial thought was to
                                        @Dialog1 *d1 = new Dialog1;
                                        Dialog2 *d2 = new Dialog2;
                                        QString title = "title test1";
                                        connect(d2, SIGNAL(on_pushButton_clicked()), this, SLOT(addNewTab(d1, title)));@

                                        instead of

                                        @ Dialog1 *d1 = new Dialog1;
                                        Dialog2 *d2 = new Dialog2;
                                        QString title = "title test1";

                                        connect(d2, SIGNAL(on_pushButton_clicked()), this, SLOT(addNewTab(QWidget* , QString&)));
                                        

                                        @

                                        but this is not feasible because the way the slot is defined
                                        @void addNewTab(QWidget *page, QString &title)@
                                        is not the same with
                                        @connect(d2, SIGNAL(on_pushButton_clicked()), this, SLOT(addNewTab(d1, title)));@

                                        here I received the error: "QObject::connect: No such slot MainWindow::show(addNewTab(QWidget*, QString&))"

                                        in this case,
                                        @connect(d2, SIGNAL(on_pushButton_clicked()), this, SLOT(addNewTab(QWidget* , QString&)));@
                                        i receive the error: "QObject::connect: Incompatible sender/receiver arguments"

                                        does this make sense?
                                        so, what's the correct way to do it? thank you for your help

                                        1 Reply Last reply
                                        0
                                        • SGaistS Offline
                                          SGaistS Offline
                                          SGaist
                                          Lifetime Qt Champion
                                          wrote on last edited by
                                          #20

                                          You can't pass parameters in connect statements (please take some time to read the "Signals And Slots" chapter in Qt's documentation.

                                          What's the use of Dialog2 ? Get a string from the user ? Then call exec on the dialog. Check it's return value. If it's okay then call directly addNewTab retrieving the string from Dialog2 through a getter.

                                          Interested in AI ? www.idiap.ch
                                          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                                          1 Reply Last reply
                                          0

                                          • Login

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