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. The slot is called after it is disconnected or blocked.
Forum Updated to NodeBB v4.3 + New Features

The slot is called after it is disconnected or blocked.

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 4 Posters 440 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.
  • E Offline
    E Offline
    extream
    wrote on last edited by
    #1
    // .h - file
    class QtClipboard : public QMainWindow
    {
        Q_OBJECT
    public:
        QtClipboard(QWidget *parent = Q_NULLPTR);
     
    private:
        Ui::QtClipboardClass ui;
        QClipboard* pcb;
    // ...
    private slots:
        void clipboardChanged();
    //...
    }
     
    // .сpp - file
    QtClipboard::QtClipboard(QWidget *parent)
        : QMainWindow(parent)
    {
    ui.setupUi(this);
    //...
    pcb = QApplication::clipboard();
    connect(pcb, &QClipboard::dataChanged, this, &QtClipboard::clipboardChanged);
    }
     
    //...
     
    void QtClipboard::slotCopyToClipboard()
    {       
        const QListWidgetItem *item = listHistory->selectedItems().at(0);
        QString itemText = item->text();
        
        
        //pcb->disconnect(SIGNAL(dataChanged()), 0, 0); 
        //pcb->blockSignals(true);
        bool ret = pcb->disconnect(this); // returned true
        pcb->setText(itemText);   // This method continues to send a signal to the slot
        connect(pcb, &QClipboard::dataChanged, this, &QtClipboard::clipboardChanged); // if to comment this line slot will be disconnected successful!
        //pcb->blockSignals(false);
        //connect(pcb, &QClipboard::dataChanged, this, &QtClipboard::clipboardChanged, Qt::DirectConnection);
    }
    
    jsulmJ 1 Reply Last reply
    0
    • E extream
      // .h - file
      class QtClipboard : public QMainWindow
      {
          Q_OBJECT
      public:
          QtClipboard(QWidget *parent = Q_NULLPTR);
       
      private:
          Ui::QtClipboardClass ui;
          QClipboard* pcb;
      // ...
      private slots:
          void clipboardChanged();
      //...
      }
       
      // .сpp - file
      QtClipboard::QtClipboard(QWidget *parent)
          : QMainWindow(parent)
      {
      ui.setupUi(this);
      //...
      pcb = QApplication::clipboard();
      connect(pcb, &QClipboard::dataChanged, this, &QtClipboard::clipboardChanged);
      }
       
      //...
       
      void QtClipboard::slotCopyToClipboard()
      {       
          const QListWidgetItem *item = listHistory->selectedItems().at(0);
          QString itemText = item->text();
          
          
          //pcb->disconnect(SIGNAL(dataChanged()), 0, 0); 
          //pcb->blockSignals(true);
          bool ret = pcb->disconnect(this); // returned true
          pcb->setText(itemText);   // This method continues to send a signal to the slot
          connect(pcb, &QClipboard::dataChanged, this, &QtClipboard::clipboardChanged); // if to comment this line slot will be disconnected successful!
          //pcb->blockSignals(false);
          //connect(pcb, &QClipboard::dataChanged, this, &QtClipboard::clipboardChanged, Qt::DirectConnection);
      }
      
      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by jsulm
      #2

      @extream said in The slot is called after it is disconnected or blocked.:

      connect(pcb, &QClipboard::dataChanged, this, &QtClipboard::clipboardChanged);

      This is your second connect for that signal/slot.
      First one is in constructor.

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1
      • E Offline
        E Offline
        extream
        wrote on last edited by
        #3

        Yes, first time i call connect(pcb, &QClipboard::dataChanged, this, &QtClipboard::clipboardChanged) in the constructor

        jsulmJ JonBJ 2 Replies Last reply
        0
        • E extream

          Yes, first time i call connect(pcb, &QClipboard::dataChanged, this, &QtClipboard::clipboardChanged) in the constructor

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @extream But why do you connect it twice?

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          2
          • E extream

            Yes, first time i call connect(pcb, &QClipboard::dataChanged, this, &QtClipboard::clipboardChanged) in the constructor

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

            @extream
            As @jsulm has said, you should not be connecting (same signal/slot) twice. Note also from the docs:

            By default, a signal is emitted for every connection you make; two signals are emitted for duplicate connections. You can break all of these connections with a single disconnect() call. If you pass the Qt::UniqueConnection type, the connection will only be made if it is not a duplicate. If there is already a duplicate (exact same signal to the exact same slot on the same objects), the connection will fail and connect will return an invalid QMetaObject::Connection.

            1 Reply Last reply
            1
            • VRoninV Offline
              VRoninV Offline
              VRonin
              wrote on last edited by VRonin
              #6

              You just need to wait 1 event loop cycle before reconnecting:
              add a private member to the class QMetaObject::Connection clipConn;

              QtClipboard::QtClipboard(QWidget *parent)
                  : QMainWindow(parent)
              {
              ui.setupUi(this);
              //...
              clipConn = QObject::connect(QApplication::clipboard(), &QClipboard::dataChanged, this, &QtClipboard::clipboardChanged);
              }
              
              void QtClipboard::slotCopyToClipboard()
              {       
                  const QString itemText = listHistory->selectedItems().at(0)->text();
                  QObject::disconnect(clipConn);
                  QApplication::clipboard()->setText(itemText);
                  QTimer::singleShot(0,this,[this](){
                      if(!clipConn)
                          clipConn = QObject::connect(QApplication::clipboard(), &QClipboard::dataChanged, this, &QtClipboard::clipboardChanged);
                  });
              }
              

              "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
              ~Napoleon Bonaparte

              On a crusade to banish setIndexWidget() from the holy land of Qt

              E 1 Reply Last reply
              3
              • VRoninV VRonin

                You just need to wait 1 event loop cycle before reconnecting:
                add a private member to the class QMetaObject::Connection clipConn;

                QtClipboard::QtClipboard(QWidget *parent)
                    : QMainWindow(parent)
                {
                ui.setupUi(this);
                //...
                clipConn = QObject::connect(QApplication::clipboard(), &QClipboard::dataChanged, this, &QtClipboard::clipboardChanged);
                }
                
                void QtClipboard::slotCopyToClipboard()
                {       
                    const QString itemText = listHistory->selectedItems().at(0)->text();
                    QObject::disconnect(clipConn);
                    QApplication::clipboard()->setText(itemText);
                    QTimer::singleShot(0,this,[this](){
                        if(!clipConn)
                            clipConn = QObject::connect(QApplication::clipboard(), &QClipboard::dataChanged, this, &QtClipboard::clipboardChanged);
                    });
                }
                
                E Offline
                E Offline
                extream
                wrote on last edited by
                #7

                @VRonin said in The slot is called after it is disconnected or blocked.:

                QTimer::singleShot(0,this,this{
                if(!clipConn)
                clipConn = QObject::connect(QApplication::clipboard(), &QClipboard::dataChanged, this, &QtClipboard::clipboardChanged);
                });

                Tahnks! It work!

                1 Reply Last reply
                1

                • Login

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