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. currentChanged fires immediately upon initialization?
Forum Updated to NodeBB v4.3 + New Features

currentChanged fires immediately upon initialization?

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 4 Posters 820 Views 2 Watching
  • 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.
  • S Offline
    S Offline
    SRaD
    wrote on last edited by SRaD
    #1

    I have a class that is derived from QTreeView and I want to get notified that a selection has occurred in the view. The problem is that currentChanged is fired immediately upon the startup of my app where my views are being initialized, as if a row was already selected, which it hasn't.

    How does one go about not having currentChanged fire till a user selects a row, and not upon initialization?

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

      Hi,

      Can you show how you are setting up your widgets ?

      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
      • S Offline
        S Offline
        SRaD
        wrote on last edited by SRaD
        #3
        class CarsWidget : public QWidget { ... };
        class CarsTreeView : public QTreeView { ... };
        
        CarsWidget::CarsWidget(MainWindow *win, QWidget *parent) : main_win(win), QWidget(parent)
        {
            view = new CarsTreeView(win);
            view->setModel(model);
        
            QVBoxLayout *carsLayout = new QVBoxLayout;
            carsLayout->addWidget(view);
            carsLayout->setContentsMargins(0, 0, 0, 5);
        
            setLayout(carsLayout);
        } 
        
        CarsTreeView::CarsTreeView(MainWindow *win, QWidget *parent) : mainWin(win), QTreeView(parent) { }
        
        void CarsTreeView::currentChanged(const QModelIndex &current, const QModelIndex &previous)
        {
            cout << "currentChanged()\n";
            win->changeSelectedTab("Wheels");
        }
        

        The currentChanged() function is fired as soon as all this is created. My intention is that, when the user clicks on a row, a new tab will appear, but the tab appears too soon as this is fired upon creation.

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

          You should check the validate of the index passed and react properly on that.

          From the portion of code you are showing, there's an architectural problem that is going to make your application hard to maintain. Why does the CarsTreeView class need to know the QMainWindow ? You are creating an unnecessary tight coupling here.

          If you want something to happen when currentChanged is called, you should rather emit a message from that function and connect your main window to it. It's not the responsibility of a widget to modify a parent.

          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
          2
          • S Offline
            S Offline
            SRaD
            wrote on last edited by SRaD
            #5

            Ah, ok, that makes sense.

            And this brings up a good point that I'm not quite clear on.

            The reason I pass the MainWindow into CarsTreeView is because, if I understand correctly, to emit a signal to a slot means you must have the object to emit the signal to. E.g., ...

            connect(got_selection, &CarsTreeView::send_my_selection,
                mainWin, &MainWindow::show_tab);
            

            But I can't see the difference in doing

            emit show_tab(idx);
            

            versus ...

            mainWin->show_tab(idx);
            
            jsulmJ Pablo J. RoginaP 2 Replies Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              show_tab is a slot, you don't emit a slot, you emit a signal.

              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
              1
              • S SRaD

                Ah, ok, that makes sense.

                And this brings up a good point that I'm not quite clear on.

                The reason I pass the MainWindow into CarsTreeView is because, if I understand correctly, to emit a signal to a slot means you must have the object to emit the signal to. E.g., ...

                connect(got_selection, &CarsTreeView::send_my_selection,
                    mainWin, &MainWindow::show_tab);
                

                But I can't see the difference in doing

                emit show_tab(idx);
                

                versus ...

                mainWin->show_tab(idx);
                
                jsulmJ Offline
                jsulmJ Offline
                jsulm
                Lifetime Qt Champion
                wrote on last edited by
                #7

                @SRaD said in currentChanged fires immediately upon initialization?:

                if I understand correctly, to emit a signal to a slot means you must have the object to emit the signal to

                This is wrong. You only need the object to connect a signal to a slot. Emitting signal does not require to know who is connected to the signal. That is one of the advantages of signals/slots. The connect() method should be called inside main window - here you have both objects to connect.

                "But I can't see the difference in doing" - connect does not call anything (it does NOT emit signal). All it does is connecting a signal to a slot. If you later do "emit someSignal()" then all slots connected to that signal will be called.

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

                1 Reply Last reply
                3
                • S SRaD

                  Ah, ok, that makes sense.

                  And this brings up a good point that I'm not quite clear on.

                  The reason I pass the MainWindow into CarsTreeView is because, if I understand correctly, to emit a signal to a slot means you must have the object to emit the signal to. E.g., ...

                  connect(got_selection, &CarsTreeView::send_my_selection,
                      mainWin, &MainWindow::show_tab);
                  

                  But I can't see the difference in doing

                  emit show_tab(idx);
                  

                  versus ...

                  mainWin->show_tab(idx);
                  
                  Pablo J. RoginaP Offline
                  Pablo J. RoginaP Offline
                  Pablo J. Rogina
                  wrote on last edited by
                  #8

                  @SRaD you may want to think of connect, signal and slot as wiring, switch and bulb respectively.

                  With connect() you wire the switch to the bulb, then when you emit a signal is like actuating the switch so the electricity ends up in the bulb (the slot) where you carry on things (light your room).

                  Upvote the answer(s) that helped you solve the issue
                  Use "Topic Tools" button to mark your post as Solved
                  Add screenshots via postimage.org
                  Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

                  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