Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I have a QGIS plugin written in Python which uses a MainWindow to hold a few other widgets. When the window first gets shown, some widgets are drawn and some are not. When I resize or minimise/restore the dialog, everything is drawn correctly.

What's going on?

Immediately after dialog shown: Immediately after dialog shown

After minimise/restore or resize: After minimise/resize

from PyQt4 import QtGui
from PyQt4 import QtCore
from qgis.core import *
from qgis.gui import *
from ui_townplanning import Ui_TownPlanning
# from townplanning import msgbox
# create the dialog for zoom to point

townplanning.py: class TownPlanning:

    def __init__(self, iface):
        # Save reference to the QGIS interface
        self.iface = iface
        self.canvas = self.iface.mapCanvas()
        # initialize plugin directory
        self.plugin_dir = os.path.dirname(__file__)
        # initialize locale
        locale = QtCore.QSettings().value("locale/userLocale")[0:2]
        localePath = os.path.join(self.plugin_dir, 'i18n',
                                  'townplanning_{}.qm'.format(locale))

        if os.path.exists(localePath):
            self.translator = QTranslator()
            self.translator.load(localePath)

            if qVersion() > '4.3.3':
                QCoreApplication.installTranslator(self.translator)

        self.username = None
        self.data = None


    def initGui(self):
        # Create action that will start plugin configuration
        self.toolbar = self.iface.addToolBar("TownPlanningToolbar")
        self.action = QtGui.QAction(
            QtGui.QIcon(":/plugins/townplanning/icon.png"),
            u"Town Planning", self.iface.mainWindow())
        # connect the action to the run method
        self.action.triggered.connect(self.run)

        # Add toolbar button and menu item
        self.toolbar.addAction(self.action)
        self.iface.addPluginToMenu(u"&Town Planning", self.action)

    def unload(self):
        # Remove the plugin menu item and icon
        self.iface.removePluginMenu(u"&Town Planning", self.action)
        del self.toolbar

    # run method that performs all the real work
    def run(self):
        self.dlg = TownPlanningDialog(self.iface, layeradded)
        self.dlg.show()
        self.dlg.repaint()

townplanningdialog.py:

class TownPlanningDialog(QtGui.QMainWindow):
    def __init__(self, iface, layer):
        QtGui.QMainWindow.__init__(self)
        # Set up the user interface from Designer.
        self.ui = Ui_TownPlanning()
        self.ui.setupUi(self)
        self.iface = iface
        self.canvas = self.iface.mapCanvas()
        self.band = None

        self.ui.previewCanvas = QgsMapCanvas(self.ui.widget)
        self.ui.previewCanvas.setCanvasColor(QtCore.Qt.white)
        self.ui.previewCanvas.enableAntiAliasing(True)
        self.ui.previewCanvas.resize(self.ui.widget.size())

        self.ui.previewCanvas.setLayerSet([QgsMapCanvasLayer(layer)])
        self.zoom_extents_preview_canvas()

        self.treeWidget = self.ui.treeWidget
        self.lineEditFilter = self.ui.lineEdit
        self.dataLabel = self.ui.label
        self.zoomAllButton = self.ui.pushButton
        self.viewBAButton = self.ui.pushButton_2
        self.zoomSelectionCheckBox = self.ui.checkBox
        self.noRecordsLabel = self.ui.label_3
        self.searchMethodCombo = self.ui.comboBox
        self.zoomInButton = self.ui.pushButton_3
        self.zoomOutButton = self.ui.pushButton_4
        self.panButton = self.ui.pushButton_5

ui_townplanning.py:

class Ui_TownPlanning(object):
    def setupUi(self, TownPlanning):
        TownPlanning.setObjectName(_fromUtf8("TownPlanning"))
        TownPlanning.resize(793, 605)
        TownPlanning.setCursor(QtCore.Qt.ArrowCursor)
        self.centralwidget = QtGui.QWidget(TownPlanning)
        self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
        self.lineEdit = QtGui.QLineEdit(self.centralwidget)
        self.lineEdit.setGeometry(QtCore.QRect(10, 60, 161, 21))
        self.lineEdit.setText(_fromUtf8(""))
        self.lineEdit.setEchoMode(QtGui.QLineEdit.Normal)
        self.lineEdit.setObjectName(_fromUtf8("lineEdit"))
        self.treeWidget = QtGui.QTreeWidget(self.centralwidget)
        self.treeWidget.setGeometry(QtCore.QRect(10, 80, 161, 192))
        self.treeWidget.setProperty(_fromUtf8("showDropIndicator"), False)
        self.treeWidget.setDragEnabled(False)
        self.treeWidget.setDragDropOverwriteMode(False)
        self.treeWidget.setDragDropMode(QtGui.QAbstractItemView.NoDragDrop)
        self.treeWidget.setAlternatingRowColors(True)
        self.treeWidget.setSelectionMode(QtGui.QAbstractItemView.SingleSelection)
        self.treeWidget.setObjectName(_fromUtf8("treeWidget"))
        self.treeWidget.header().setVisible(False)
        self.treeWidget.header().setSortIndicatorShown(True)
        self.groupBox = QtGui.QGroupBox(self.centralwidget)
        self.groupBox.setGeometry(QtCore.QRect(380, 20, 401, 101))
        self.groupBox.setObjectName(_fromUtf8("groupBox"))
        self.label = QtGui.QLabel(self.groupBox)
        self.label.setGeometry(QtCore.QRect(10, 20, 381, 71))
        self.label.setText(_fromUtf8(""))
        self.label.setObjectName(_fromUtf8("label"))
        self.widget = QtGui.QWidget(self.centralwidget)
        self.widget.setGeometry(QtCore.QRect(380, 160, 400, 400))
        self.widget.setObjectName(_fromUtf8("widget"))
        self.pushButton_5 = QtGui.QPushButton(self.widget)
        self.pushButton_5.setGeometry(QtCore.QRect(50, 10, 24, 24))
        self.pushButton_5.setText(_fromUtf8(""))
        self.pushButton_5.setObjectName(_fromUtf8("pushButton_5"))
        self.pushButton_3 = QtGui.QPushButton(self.widget)
        self.pushButton_3.setGeometry(QtCore.QRect(2, 10, 24, 24))
        self.pushButton_3.setText(_fromUtf8(""))
        self.pushButton_3.setObjectName(_fromUtf8("pushButton_3"))
        self.pushButton_4 = QtGui.QPushButton(self.widget)
        self.pushButton_4.setGeometry(QtCore.QRect(26, 10, 24, 24))
        self.pushButton_4.setText(_fromUtf8(""))
        self.pushButton_4.setObjectName(_fromUtf8("pushButton_4"))
        self.pushButton = QtGui.QPushButton(self.centralwidget)
        self.pushButton.setGeometry(QtCore.QRect(10, 330, 75, 23))
        self.pushButton.setObjectName(_fromUtf8("pushButton"))
        self.pushButton_2 = QtGui.QPushButton(self.centralwidget)
        self.pushButton_2.setGeometry(QtCore.QRect(10, 300, 101, 23))
        self.pushButton_2.setObjectName(_fromUtf8("pushButton_2"))
        self.checkBox = QtGui.QCheckBox(self.centralwidget)
        self.checkBox.setGeometry(QtCore.QRect(10, 280, 111, 18))
        self.checkBox.setObjectName(_fromUtf8("checkBox"))
        self.comboBox = QtGui.QComboBox(self.centralwidget)
        self.comboBox.setEnabled(True)
        self.comboBox.setGeometry(QtCore.QRect(10, 30, 161, 22))
        self.comboBox.setObjectName(_fromUtf8("comboBox"))
        self.comboBox.addItem(_fromUtf8(""))
        self.comboBox.addItem(_fromUtf8(""))
        self.label_2 = QtGui.QLabel(self.centralwidget)
        self.label_2.setGeometry(QtCore.QRect(10, 10, 71, 16))
        self.label_2.setObjectName(_fromUtf8("label_2"))
        self.label_3 = QtGui.QLabel(self.centralwidget)
        self.label_3.setGeometry(QtCore.QRect(20, 90, 101, 16))
        self.label_3.setObjectName(_fromUtf8("label_3"))
        TownPlanning.setCentralWidget(self.centralwidget)
        self.menubar = QtGui.QMenuBar(TownPlanning)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 793, 18))
        self.menubar.setObjectName(_fromUtf8("menubar"))
        self.menuSearch = QtGui.QMenu(self.menubar)
        self.menuSearch.setObjectName(_fromUtf8("menuSearch"))
        TownPlanning.setMenuBar(self.menubar)
        self.statusbar = QtGui.QStatusBar(TownPlanning)
        self.statusbar.setObjectName(_fromUtf8("statusbar"))
        TownPlanning.setStatusBar(self.statusbar)
        self.action1 = QtGui.QAction(TownPlanning)
        self.action1.setObjectName(_fromUtf8("action1"))
        self.action2 = QtGui.QAction(TownPlanning)
        self.action2.setObjectName(_fromUtf8("action2"))
        self.menuSearch.addAction(self.action1)
        self.menuSearch.addAction(self.action2)
        self.menubar.addAction(self.menuSearch.menuAction())

        self.retranslateUi(TownPlanning)
        QtCore.QMetaObject.connectSlotsByName(TownPlanning)
share|improve this question
    
For some reason there are paint events missing. I guess calling mainWindow.repaint() once should workaround this problem. –  Matthias Kuhn Dec 3 '13 at 9:14
    
That doesn't seem to work. I call it just after my mainWindow.show(). –  James N Dec 3 '13 at 9:34
    
Can you add the relevant lines of your code –  Matthias Kuhn Dec 3 '13 at 9:58
    
Added. Excuse the volume, I added everything I could from the plugin to prove that nothing was changed in the ui module. –  James N Dec 3 '13 at 10:36
    
This could be a more generic problem in QGIS than your code. I often have to minimize then maximize QGIS to redraw the attribute window and processing window correctly, when they are opened the first time. –  Jakob Dec 3 '13 at 11:46
show 1 more comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.