I build a plugin. It has a lot of comboboxes (QgsMapLayerComboBox
). They are all empty when no project is loaded. When I open a project in QGIS, a layer is automatically shown in all comboboxes. All is the same layer.
I don't want a layer is selected. How can I avoid that?
A sample form and script for QGIS Python Editor:
from qgis.PyQt.QtCore import *
from qgis.PyQt.QtGui import *
from qgis.PyQt.QtWidgets import *
class Window(QMainWindow):
def __init__(self):
super(Window, self).__init__()
self.setGeometry(250, 250, 300, 100)
self.setWindowTitle("Test")
cb1 = QgsMapLayerComboBox(self)
cb2 = QgsMapLayerComboBox(self)
layout = QVBoxLayout()
layout.addWidget(cb1)
layout.addWidget(cb2)
contents = QWidget()
contents.setLayout(layout)
self.setCentralWidget(contents)
window = Window()
window.setWindowFlags(window.windowFlags() | Qt.WindowStaysOnTopHint)
window.show()
None
. I don't remember the name of the method but it something like.setCurrentLayer()
. TheQgsMapLayerComboBox
also has an option to allow an empty entry, that must be enabled too.