Question

I use Qt Designer to build my UI layout. On the layout, I have a combobox named cb_fac_cd. In my code I have a function that will automatically build a combobox based on the list I'm attempting to build. I have a lot of these lists defined in the database and this function spits out a QComboBox.

Unfortunately, until now I've only used this function to add cellWidgets to QTableWidgets. It works perfectly there. Now I want to populate this preexisting combobox.

It seems that a simple self.ui.cb_fac_cd = makeComboBox('FACILITIES') doesn't work. I can see that the function returns the QComboBox as usual, but the cb_fac_cd combobox remains unpopulated.

How can I copy or assign the returned combobox to the one build in Qt Designer?

I am using PyQt, but that shouldn't make any difference.

Was it helpful?

Solution

As far as I know, you cannot replace objects which are part of the .h files generated by Qt's UIC mechanism (though I cannot confirm that 100% right now).

What I usually do in such cases is to have an empty layout in the ui file and then do the following: (Note that I'm using Qt/C++ syntax as I don't know the pyqt syntax but I think you will get the idea)

QComboBox* pNewComboBox = makeComboBox( "FACILITIES" );
ui.pComboBoxLayout->addWidget( pComboBox );

Additionally, if possible for your program, consider using an enumeration rather than a string for your makeComboBox function. This is usually faster and safer.

OTHER TIPS

It sounds like you should either

  1. Change makeComboBox to populateComboBox(QComboBox *p) and pass it the combobox in your layout to fill, or
  2. Create and add the QComboBox to your layout dynamically and remove it from your .ui

You can change the model from you Ui to the one you get from the other combobox:

tempCombo = makeComboBox( "FACILITIES" )
self.ui.cb_fac_cd.setModel(tempCombo.model())

But for that part:

I have a lot of these lists defined in the database and this function spits out a QComboBox. Unfortunately, until now I've only used this function to add cellWidgets to QTableWidgets.

If the data comes from a database, you might want to look at QSqlRelationalTableModel to avoid doing that manually.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top