1

I am in my first steps in PyQt and I face difficulties in how to run the following code within QGIS console. I want to create a simple window, but nothing happens. I change main by console and then QGIS crashes. Any help is welcome.

import sys
from PyQt5.QtWidgets import QApplication, QWidget

def main():
  app = QApplication(sys.argv)
  w = QWidget()
  w.resize(250, 150)
  w.move(300, 300)
  w.setWindowTitle('Simple')
  w.show()
  sys.exit(app.exec_())

if __name__ == '__main__':
  main()
4
  • 2
    i think in QGIS you can't use an application object, because qgis itself is a QtApplication. Try to execute lines 6-10 alone and you'll see a widget on the screen. To have a full functional dialog, you'll need to a have something like a qgis-plugin. To work with pyqt only, try an standalone python shell or an ide. Aug 13, 2018 at 14:42
  • Many thanks Andreas for your clarifications. In fact, I am used to write my codes in Notepad++ and run them in QGis console. As you mentioned, it is much better to create a QGis-plugin. What is the steps to follow to achieve this task easily?
    – ennine
    Aug 14, 2018 at 13:56
  • try the qgis plugin "plugin builder", there is also a tutorial on this topic, but not for qgis3 (some steps are different). But Plugin Builder will guide you through the process... Aug 14, 2018 at 14:09
  • I put my comments into an answer, accept it if you like! Aug 14, 2018 at 16:34

1 Answer 1

4

In QGIS you can't use an application object, because qgis itself is a QtApplication. Try to execute lines 6-10 alone and you'll see a widget on the screen. To have a full functional dialog, you'll need to a have something like a qgis-plugin. To work with pyqt only, try an standalone python shell or an ide.

For plugin creation, you use the qgis plugin "plugin builder", there is also a tutorial on this topic (http://www.qgistutorials.com/de/docs/building_a_python_plugin.html), but it doesn't cover qgis3 (some steps are different). But Plugin Builder will guide you through the process...

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.