Page 1 of 2 12 LastLast
Results 1 to 20 of 29

Thread: tableview and pushbutton connection

  1. #1
    Join Date
    Mar 2009
    Posts
    104
    Qt products
    Qt4
    Platforms
    MacOS X
    Thanked 1 Time in 1 Post

    Default tableview and pushbutton connection

    I'm trying to make the button enabled when some of the tableview row is selected

    1. I'm trying to figure out how to get the right signal from the tableview
    2. I can't understand why qtcreator underline with red second line??

    Qt Code:
    1. connect(ui->tableView_clients, SIGNAL(what_should_be_here?),
    2. ui->pushButton_select, SLOT(setEnabled(true)));
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android
    Thanks
    62
    Thanked 260 Times in 246 Posts

    Default Re: tableview and pushbutton connection

    1) I think you can use selectedIndexes and emit your own signal (create a signal that emits a bool and connect it with your slot).
    2) The connect only take parameter type not value, so the true is error and bool is correct.

  3. #3
    Join Date
    Mar 2009
    Posts
    104
    Qt products
    Qt4
    Platforms
    MacOS X
    Thanked 1 Time in 1 Post

    Default Re: tableview and pushbutton connection

    OK, but when to emit the signal? I want the slot to be triggered when row is selected!So i thing i should use an existed signal.

  4. #4
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Thanked 370 Times in 336 Posts

    Default Re: tableview and pushbutton connection

    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  5. #5
    Join Date
    Nov 2009
    Posts
    129
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    4
    Thanked 29 Times in 29 Posts

    Default Re: tableview and pushbutton connection

    Quote Originally Posted by unix7777 View Post
    I'm trying to make the button enabled when some of the tableview row is selected
    This one is a bit more complicated than you would expect. The signal you need doesn’t come directly from QTableView, but from the QItemSelectionModel associated with it.

    Qt Code:
    1. connect(ui->tableView_clients->selectionModel(),SIGNAL(currentRowChanged(QModelIndex, QModelIndex)),SLOT(enableCheck(QModelIndex, QModelIndex)));
    2.  
    3. ...
    4.  
    5. void enableCheck(const QModelIndex& current, const QModelIndex& previous) {
    6. ui->pushButton_select->setEnabled(current.isValid());
    7. }
    To copy to clipboard, switch view to plain text mode 
    Since setEnabled requires a bool argument, and the signal you need doesn’t have one, you can’t connect them directly.

    Zlatomir’s idea will also work, though he named the wrong method. QAbstractItemView::selectionChanged is a virtual protected slot that is called when the selection changes. If it isn’t already, you would have to make tableView_clients an instance of a subclass of QTableView — remembering the Q_OBJECT macro! — and define a signal which you would connect to QPushButton::setEnabled(bool); then override the selectionChanged method and have it call the base method, then emit that signal.

  6. #6
    Join Date
    Mar 2009
    Posts
    104
    Qt products
    Qt4
    Platforms
    MacOS X
    Thanked 1 Time in 1 Post

    Default Re: tableview and pushbutton connection

    Sorry but i can't figure out how to emit the signal.Do i have to make two connect() one with slot that will emit the signal and another that receive bool from the custom slot.
    I'm confused!

    I did the following but nothing happens with this signal!With another the slot works:

    Qt Code:
    1. connect(ui->tableView_clients->selectionModel(), SIGNAL(currentRowChanged(QModelIndex,QModelIndex)),
    2. this, SLOT(setBool()));
    To copy to clipboard, switch view to plain text mode 

    and the custom slot is:

    Qt Code:
    1. void Clients::setBool()
    2. {
    3. ui->pushButton_select->setEnabled(true);
    4. }
    To copy to clipboard, switch view to plain text mode 

    Please help to find the right signal
    Last edited by unix7777; 31st August 2012 at 21:10.

  7. #7
    Join Date
    Nov 2009
    Posts
    129
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    4
    Thanked 29 Times in 29 Posts

    Default Re: tableview and pushbutton connection

    Edit: I missed your edit, unix7777. You’re going about it spirit’s way, so what I wrote originally doesn’t apply.

    Quote Originally Posted by unix7777 View Post
    I did the following but nothing happens with this signal!With another the slot works:

    Qt Code:
    1. connect(ui->tableView_clients->selectionModel(), SIGNAL(currentRowChanged(QModelIndex,QModelIndex)),
    2. this, SLOT(setBool()));
    To copy to clipboard, switch view to plain text mode 

    and the custom slot is:

    Qt Code:
    1. void Clients::setBool()
    2. {
    3. ui->pushButton_select->setEnabled(true);
    4. }
    To copy to clipboard, switch view to plain text mode 

    Please help to find the right signal
    I’m pretty sure you have the right signal. I used this (direct copy from my code) in a recent project:

    Qt Code:
    1. connect(ui->remTable->selectionModel(),SIGNAL(currentRowChanged(QModelIndex,QModelIndex)),
    2. SLOT(setClean()));
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. void MainWindow::setClean() {
    2. bool vi = rtProxy.rowCount() > 0;
    3. if (vi && !ui->remTable->currentIndex().isValid())
    4. ui->remTable->setCurrentIndex(rtProxy.index(0,0));
    5. if (vi)
    6. remToUI(remind()->reminders[rtProxy.mapToSource(ui->remTable->currentIndex()).row()]);
    7. ui->remTable ->setEnabled(vi );
    8. ui->actionImport ->setEnabled(true );
    9. ui->actionExport ->setEnabled(vi );
    10. ui->actionClearExpired->setEnabled(vi );
    11. ui->actionNew ->setEnabled(true );
    12. ui->actionCancel ->setEnabled(false);
    13. ui->actionDelete ->setEnabled(vi );
    14. ui->actionReplace ->setEnabled(false);
    15. ui->actionSave ->setEnabled(false);
    16. ui->reminderType ->setEnabled(vi );
    17. ui->textGroupBox ->setEnabled(vi );
    18. ui->settingsGroupBox ->setEnabled(vi );
    19. if (vi) ui->remTable->setFocus();
    20. else ui->buttonNew->setFocus();
    21. checkIntro();
    22. }
    To copy to clipboard, switch view to plain text mode 
    and if it weren’t working... I’d definitely know it. So I think something else must be the problem.

    This is probably not the problem: but just to be sure, if you haven’t already, do a complete rebuild. I’m not sure if it’s something peculiar to my setup, but in my last Qt project I frequently found that the build process would apparently miss something that needed recompilation, and the result would be some strange behavior that made no sense looking at the code. I eventually got to where whenever I couldn’t see within a few minutes what I had done wrong, I’d try a full rebuild. It was only the explanation maybe one out of ten times, but it still saved a lot of frustration!

    If that’s not it... are you running a debug build, and are you checking the output to be sure there are no run-time error messages related to the connect?

    If you aren’t already, try doing the connect as late as possible. I’m not sure about this, but I’m thinking perhaps there are things that can change the selectionModel of the QTableView. If it changed after the connect, nothing would notify you about that, but the connection would no longer be effective.

    (original below)

    Quote Originally Posted by unix7777 View Post
    Sorry but i can't figure out how to emit the signal.Do i have to make two connect() one with slot that will emit the signal and another that receive bool from the custom slot.
    I'm confused!
    You’ll only need one connect.

    If you want to emit your own signal (Zlatomir’s method as opposed to spirit’s), the place to do it is in QAbstractItemView::selectionChanged.

    That is a virtual method, and QAbstractItemView is a public base of QTableView, from which you would derive the class for your control.

    If you don’t follow the previous sentence and its implications (for example, if you’re relatively new to C++ and don’t quite understand the concept of virtual methods), tell us that. You could hardly help being confused in that case.

    Otherwise, remember to use the Q_OBJECT macro in the declaration for the class you derive from QTableView, and define the signal you will emit in a signals section.
    Last edited by Coises; 31st August 2012 at 22:31. Reason: updated contents

  8. #8
    Join Date
    Mar 2009
    Posts
    104
    Qt products
    Qt4
    Platforms
    MacOS X
    Thanked 1 Time in 1 Post

    Default Re: tableview and pushbutton connection

    I still see the slot working.I have try with another signal and it works:
    Qt Code:
    1. connect(ui->lineEdit_search_clients, SIGNAL(textChanged(QString)),
    2. this, SLOT(setBool()));
    To copy to clipboard, switch view to plain text mode 

    I also try to completely rebuild the project.Of course i observe the debugger output.There is nothing.
    Also i don't need to check whether selection has changed, but whether some row is selected.If is selected the button should be enabled if not disabled

  9. #9
    Join Date
    Nov 2009
    Posts
    129
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    4
    Thanked 29 Times in 29 Posts

    Default Re: tableview and pushbutton connection

    Quote Originally Posted by unix7777 View Post
    Also i don't need to check whether selection has changed, but whether some row is selected.If is selected the button should be enabled if not disabled
    Hmmm... I wonder if the clue is in there, somewhere.

    I (and the others, I think) presumed you wanted to know when the QTableView’s state changes in such a way that a row is selected (when none was before; perhaps also when a different row was selected before); and perhaps also when it changes such that nothing is selected, when something was selected before. A signal is only going to be triggered when something happens or changes. If it’s not the selection changing that should trigger your check, what should trigger your check?

    For the check itself, ui->tableView_clients->currentIndex().isValid() will tell you whether anything is selected.

  10. #10
    Join Date
    Mar 2009
    Posts
    104
    Qt products
    Qt4
    Platforms
    MacOS X
    Thanked 1 Time in 1 Post

    Default Re: tableview and pushbutton connection

    OK, but which in this case is the signal?

    Qt Code:
    1. connect(ui->tableView_clients->currentIndex(), SIGNAL(isValid()),
    2. this, SLOT(setBool()));
    To copy to clipboard, switch view to plain text mode 

    Doesn't work

  11. #11
    Join Date
    Nov 2009
    Posts
    129
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    4
    Thanked 29 Times in 29 Posts

    Default Re: tableview and pushbutton connection

    Quote Originally Posted by unix7777 View Post
    OK, but which in this case is the signal?
    I know this might seem like a stupid question — and perhaps it is — but:

    When do expect the enabled/disabled status of pushButton_select to change?
    What would happen that should cause it to become disabled, and what would happen that should cause it to become enabled?

    I’m trying to get a picture of the context of this within the logic of your application, so that perhaps I can guess why something that seems like it should be working is not.

    (The code you tried can’t work for two reasons: QModelIndex::isValid() isn’t a signal, and the value of currentIndex() will change, so having connected to the QModelIndex that was current when you did the connect won’t help once it does change. I meant that if, as you indicated, you need to enable when something is selected and disable when nothing is selected, ui->tableView_clients->currentIndex().isValid() is what will tell you whether to enable or disable. When to check that seems to be something we haven’t figured out yet.)

  12. #12
    Join Date
    Mar 2009
    Posts
    104
    Qt products
    Qt4
    Platforms
    MacOS X
    Thanked 1 Time in 1 Post

    Default Re: tableview and pushbutton connection

    Does it mean you that i have to make my own signal for that?
    Custom slot, custom signal, i tough there should be an easier way.This is something common.To understand the logic is this: i have a table and have 4 buttons.One for add new row, one for delete a row and one that edit the row.The 4th button is for selecting the row.When row is selected the data from the selected row is displayed on another widget.The problem is that is the user click on the select button before select a row the program crashes.This is why i want to make this check, i want the select button to be enabled only when some row is selected and when is not to be disabled.

  13. #13
    Join Date
    Nov 2009
    Posts
    129
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    4
    Thanked 29 Times in 29 Posts

    Default Re: tableview and pushbutton connection

    Quote Originally Posted by unix7777 View Post
    i have a table and have 4 buttons.One for add new row, one for delete a row and one that edit the row.The 4th button is for selecting the row.When row is selected the data from the selected row is displayed on another widget.The problem is that is the user click on the select button before select a row the program crashes.This is why i want to make this check, i want the select button to be enabled only when some row is selected and when is not to be disabled.
    Then your connect from before:
    Qt Code:
    1. connect(ui->tableView_clients->selectionModel(), SIGNAL(currentRowChanged(QModelIndex,QModelIndex)),
    2. this, SLOT(setBool()));
    To copy to clipboard, switch view to plain text mode 
    should be right, but the slot should look like this:
    Qt Code:
    1. void Clients::setBool()
    2. {
    3. ui->pushButton_select->setEnabled(ui->tableView_clients->currentIndex().isValid());
    4. }
    To copy to clipboard, switch view to plain text mode 
    so the button gets enabled when there is a selection and disabled when there is none.

    I’m assuming you’ve called (or set in Designer) ui->tableView_clients->setSelectionBehavior(QAbstractItemView::SelectRows) so that only rows can be selected. (If the user is supposed to be able to select either individual items or full rows and the problem is distinguishing specifically when a full row is selected, that would be a bit different.)

    Be sure you’re doing the connect after you’ve attached the model and set all the other characteristics of tableView_clients, to be certain you don’t inadvertently do something that replaces the selectionModel after the connect. Also don’t forget that the signal will only reach the slot for changes that occur after the connect; so you might need to call the slot explicitly just before or after the connect to be sure everything starts off right:
    Qt Code:
    1. setBool();
    2. connect(ui->tableView_clients->selectionModel(), SIGNAL(currentRowChanged(QModelIndex,QModelIndex)), this, SLOT(setBool()));
    To copy to clipboard, switch view to plain text mode 

  14. #14
    Join Date
    Mar 2009
    Posts
    104
    Qt products
    Qt4
    Platforms
    MacOS X
    Thanked 1 Time in 1 Post

    Default Re: tableview and pushbutton connection

    Qt Code:
    1. connect(ui->tableView_clients->selectionModel(), SIGNAL(currentRowChanged(QModelIndex,QModelIndex)),
    2. this, SLOT(setBool()));
    To copy to clipboard, switch view to plain text mode 

    and SLOT:

    Qt Code:
    1. void Clients::setBool()
    2. {
    3. ui->pushButton_select->setEnabled(ui->tableView_clients->currentIndex().isValid());
    4. }
    To copy to clipboard, switch view to plain text mode 

    Doesn't work.Really is very reasonable but i don't know why doesn't work.
    Yes, i set that only row can be selected not separated cell.

  15. #15
    Join Date
    Nov 2009
    Posts
    129
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    4
    Thanked 29 Times in 29 Posts

    Default Re: tableview and pushbutton connection

    Quote Originally Posted by unix7777 View Post
    Doesn't work.Really is very reasonable but i don't know why doesn't work.
    Yes, i set that only row can be selected not separated cell.
    Just to be certain: try putting a qDebug() in setBool(). I presume it is not being called when you select a row, but let’s be sure.

    Can you post the class definitions (header files) for Clients and for the class of which tableView_clients is an instance? Maybe there’s a clue there... I’m not sure where to look next.

  16. #16
    Join Date
    Mar 2009
    Posts
    104
    Qt products
    Qt4
    Platforms
    MacOS X
    Thanked 1 Time in 1 Post

    Default Re: tableview and pushbutton connection

    I don't know how to debug this!
    But as i say the slot works with another signal, so the problem is in the signal, not in the slot.
    The Tableview file is generated by the .ui file.

  17. #17
    Join Date
    Nov 2009
    Posts
    129
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    4
    Thanked 29 Times in 29 Posts

    Default Re: tableview and pushbutton connection

    Quote Originally Posted by unix7777 View Post
    I don't know how to debug this!
    But as i say the slot works with another signal, so the problem is in the signal, not in the slot.
    The Tableview file is generated by the .ui file.
    In my experience, most debugging comes down to finding what it was I was sure was true, that wasn’t.

    If you don’t have an #include <QDebug> in the Clients implementation (.cpp) file, add one. Then change the slot to:
    Qt Code:
    1. void Clients::setBool() {
    2. qDebug() << "At setBool";
    3. ui->pushButton_select->setEnabled(ui->tableView_clients->currentIndex().isValid());
    4. }
    To copy to clipboard, switch view to plain text mode 
    and let’s make sure our guess that it’s never being called is true. Do whatever you do to select a row, and to clear the selection; “At setBool” should appear every time the selection changes (including when it’s cleared entirely). If it doesn’t (which is what we’re expecting), we’ll have to sub-class the QTableView so we can add some debugging code and try to figure out if the signal is being emitted at all... then, if no, why?; if yes, why isn’t it reaching the slot?

    I’d still like to see the class declaration (header file) for Clients, in case there’s a clue there.

  18. #18
    Join Date
    Mar 2009
    Posts
    104
    Qt products
    Qt4
    Platforms
    MacOS X
    Thanked 1 Time in 1 Post

    Default Re: tableview and pushbutton connection

    QObject::connect: Cannot connect (null)::currentRowChanged(QModelIndex,QModelIndex) to Clients::setBool()
    Object::connect: No such signal QSortFilterProxyModel::hasSelection() in ../Faktura/clients.cpp:58
    Object::connect: (receiver name: 'lineEdit_search_clients')

    The problem is that my sqlite file is in the debug directory and i don't run the programm via QT Designer but clicking on app file in debug directory otherwise it can't find sqlite file.On this way i can't see the debugg output.

    Running the app from QT Designer i got the message above.

  19. #19
    Join Date
    Nov 2009
    Posts
    129
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    4
    Thanked 29 Times in 29 Posts

    Default Re: tableview and pushbutton connection

    Quote Originally Posted by unix7777 View Post
    QObject::connect: Cannot connect (null)::currentRowChanged(QModelIndex,QModelIndex) to Clients::setBool()
    Then, among other things, it sounds like ui->tableView_clients->selectionModel() is not returning a useful value.
    Is there any way to move the connect “later” in your code? I have a hunch tableView_clients isn’t fully set up at the point where you have the connect now. (In particular, the connect must come after you call ui->tableView_clients->setModel(...).) Try doing the connect as late in the setup sequence as you possibly can put it.

  20. #20
    Join Date
    Mar 2009
    Posts
    104
    Qt products
    Qt4
    Platforms
    MacOS X
    Thanked 1 Time in 1 Post

    Default Re: tableview and pushbutton connection

    This is the order i call it:
    Qt Code:
    1. Clients::Clients(QWidget *parent) :
    2. QDialog(parent),
    3. ui(new Ui::Clients)
    4. {
    5. ui->setupUi(this);
    6.  
    7.  
    8. connect(ui->tableView_clients->selectionModel(), SIGNAL(currentRowChanged(QModelIndex,QModelIndex)),
    9. this, SLOT(setBool()));
    10.  
    11. updateClientTable();
    12.  
    13. }
    To copy to clipboard, switch view to plain text mode 

    And thank you for spending your time helping me!!!!

Similar Threads

  1. PushButton
    By acuce in forum Qt Programming
    Replies: 2
    Last Post: 13th July 2012, 20:06
  2. Replies: 0
    Last Post: 11th November 2011, 19:18
  3. Replies: 3
    Last Post: 29th April 2011, 08:54
  4. How to use a QPolygon as a Pushbutton?
    By Manish.S in forum Newbie
    Replies: 4
    Last Post: 6th December 2010, 10:57
  5. Replies: 1
    Last Post: 2nd April 2010, 06:42

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.