Search code examples
qtpyqtqt-designerqcombobox

Is it possible to copy QCombobox to another QCombobox


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.


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.