Results 1 to 11 of 11

Thread: How to enable objects based on a selection from a drop box.

  1. #1
    Join Date
    Jan 2015
    Posts
    8
    Qt products
    PyQt3 PyQt4
    Platforms
    Windows

    Default How to enable objects based on a selection from a drop box.

    Hi,

    I have a tabWidget with a drop down box and 8 checkboxes.

    The drop down has 4 items in it, i'd like to link this to the checkboxes, so..
    1) All checkboxes are disabled to start with.
    2) If I select ItemA in the drop down then checkbox's 1 and 2 are enabled.
    3) If I select ItemB in the drop down then checkbox's 3 and 4 are enabled and so on.

    How do I do this in Qt desigener or must I do this problematically?

    I'm using Python 3.4 and Qt Designer 4.8.6.

    Thanks,

    John.

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to enable objects based on a selection from a drop box.

    You need a slot that is connected to the combobox's currentIndexChanged() signal and in there enable/disable buttons as you see fit.

    Cheers,
    _

  3. #3
    Join Date
    Jan 2015
    Posts
    8
    Qt products
    PyQt3 PyQt4
    Platforms
    Windows

    Default Re: How to enable objects based on a selection from a drop box.

    Thanks,

    However, If I select Edit Slots/Signals and drag an arrow from my combo box to checkbox 1. I then have this in the Signal/Slot Editor
    Sender Signal Receiver Slot
    comboBox currentIndexChanged(int) checkBox_A click() or show()

    But it just toggles check or uncheck on the checkboxes. I don't see any option where if select Item A, Item B etc from the combo box.

    Likewise, if I go in reverse (dragging an arrow from the checkbox to the combo box).

    Am i missing something?

    Thanks.

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to enable objects based on a selection from a drop box.

    Quote Originally Posted by pixxyman View Post
    Am i missing something?
    Yes, the part of where I wrote that you need a slot that does the enabling/disabling based on the index value

    Cheers,
    _

  5. #5
    Join Date
    Oct 2009
    Posts
    483
    Thanked 97 Times in 94 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to enable objects based on a selection from a drop box.

    You are trying to connect the combobox' currentIndexChanged(int) signal to a slot in one of the checkboxes. This is not a correct design: the action you need to perform is not local to one checkbox, so it should have a larger context. A simple solution is to put the slot in the class for the window containing your widgets, connect currentIndexChanged(int) to this slot, and write code in the slot to change the states of all the checkboxes.

  6. #6
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How to enable objects based on a selection from a drop box.

    You could try doing what yeye_olive suggests in Qt Designer, but it is probably easier to simply do it in the code for your tab widget class. In any case, you will still need to write the code for the slot - there is nothing in Designer that will let you graphically build the signal / slot logic to conditionally enable UI elements based on a combobox selection.

  7. #7
    Join Date
    Jan 2015
    Posts
    8
    Qt products
    PyQt3 PyQt4
    Platforms
    Windows

    Default Re: How to enable objects based on a selection from a drop box.

    ok, so my slot is going to be a method thats going to enable the checkboxes based on which value I select from the combo box?

  8. #8
    Join Date
    Oct 2009
    Posts
    483
    Thanked 97 Times in 94 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to enable objects based on a selection from a drop box.

    Quote Originally Posted by pixxyman View Post
    ok, so my slot is going to be a method thats going to enable the checkboxes based on which value I select from the combo box?
    Yes, it could look like this:
    Qt Code:
    1. void MyTabWidget::updateCheckboxes(int comboBoxIndex) {
    2. checkBox1->​setEnabled(/* Some boolean condition that depends on comboBoxIndex */);
    3. /* ... */
    4. checkBox8->​setEnabled(/* Some other boolean condition that depends on comboBoxIndex */);
    5. }
    To copy to clipboard, switch view to plain text mode 
    Note: since the beginning you have been talking about enabling/disabling checkboxes, which means controlling whether the user may interact with them; this is what QWidget::setEnabled() does. If you meant checking/unchecking the checkboxes, then you'll need to call QCheckBox::​setCheckState() instead.

  9. #9
    Join Date
    Jan 2015
    Posts
    8
    Qt products
    PyQt3 PyQt4
    Platforms
    Windows

    Default Re: How to enable objects based on a selection from a drop box.

    Yes, this is correct. If the user select ItemA from the drop down. I only want them to be able to select checkbox 1 or 2 and all the others would be disabled.

    Am i correct in thinking if I create signal with currentIndexChanged(), its not enough to just do that. As it will disable if I select any option. I need to say if currentIndexChanged() changed to ItemA, then enable checkbox 1 and 2.

  10. #10
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How to enable objects based on a selection from a drop box.

    You do not create the currentIndexChanged() signal - the combo box emits that. You need to connect your own slot to it, and in the slot you need to do something like this:

    Qt Code:
    1. // constructor
    2. MyTabWidget::MyTabWidget( QWidget * parent )
    3. : QTabWidget( parent )
    4. {
    5.  
    6. // setupUi, etc...
    7.  
    8. connect( myComboBox, &QComboBox::currentIndexChanged, this, &MyTabWidget::updateCheckboxes );
    9.  
    10. }
    11.  
    12. // slot
    13. void MyTabWidget::updateCheckboxes(int comboBoxIndex)
    14. {
    15. switch( comboBoxIndex )
    16. {
    17. case 0:
    18. // enable checkboxes base on selection of first item in combobox
    19. break;
    20.  
    21. case 1:
    22. // enable checkboxes base on selection of second item in combobox
    23. break;
    24.  
    25. // etc.
    26. default:
    27. break;
    28. }
    29. }
    To copy to clipboard, switch view to plain text mode 

    You might have to initialize things by calling updateCheckboxes( 0 ) in the constructor (using whatever the default startup index should be from the combobox). This makes sure that when the app first comes up, the checkboxes are in the correct states for whatever item appears by default in the combobox. If you set the initial index to -1 (no selection), then you will probably want to disable all of the checkboxes.

    If your combobox does not have a fixed number of items in it (for example, the choices are loaded from a file, database, or are determined in some other way), then obviously a switch statement won't work unless the "default:" clause is very smart. It does not sound like this is your problem though.

  11. #11
    Join Date
    Jan 2015
    Posts
    8
    Qt products
    PyQt3 PyQt4
    Platforms
    Windows

    Default Re: How to enable objects based on a selection from a drop box.

    Thanks for the help. I have this working now.

    // def setupUi(self, qadashboard): etc..
    QtCore.QObject.connect(self.comboBox, QtCore.SIGNAL(_fromUtf8("currentIndexChanged(int)" )), self.showMe)

    // slot
    def showMe(self):
    self.index = self.comboBox.currentIndex()
    if (self.index is 1):
    self.checkBox_1.setEnabled(1)
    self.checkBox_2.setEnabled(1)
    self.checkBox_3.setDisabled(1)
    self.checkBox_4.setDisabled(1)

    elif (self.index is 2):
    self.checkBox_3.setEnabled(1)
    self.checkBox_4.setEnabled(1)
    self.checkBox_1.setDisabled(1)
    self.checkBox_2.setDisabled(1)

    etc..

    def retranslateUi(self, qadashboard):
    I set everything to disabled in here.

    So it all works as i'd like it to. Thanks for the help.

Similar Threads

  1. Enable Drag & Drop between QTableWidget and QLineEdit
    By Infinity in forum Qt Programming
    Replies: 3
    Last Post: 3rd April 2013, 16:40
  2. Replies: 1
    Last Post: 28th February 2012, 09:54
  3. QtScript and creating non QObject based objects
    By tbscope in forum Qt Programming
    Replies: 1
    Last Post: 5th October 2011, 08:20
  4. Lumina GLSL IDE based on QtScript and dynamic Objects
    By oc2k1 in forum Qt-based Software
    Replies: 0
    Last Post: 12th August 2008, 04:12
  5. Replies: 1
    Last Post: 11th September 2007, 07:43

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.