Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Making a window with Qt

Making a window with Qt

Scheduled Pinned Locked Moved General and Desktop
3 Posts 3 Posters 1.3k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    TND5
    wrote on last edited by
    #1

    Hi, I'm new here xD

    I'm somewhat new to C++, (not to programming though) and totally new to Qt. I've been trying for the past hour to make a window that I can use QPainter on. I understand the documentation of QPainter perfectly, I just can't seem to figure out how to make a window with Qt that instances of QPainter can work on. How do I do this? My current goal is to just make a tiny self-template that makes a 400x300 white bordered window with a 50x50 black rectangle centered inside it.

    Thanks for your time!

    1 Reply Last reply
    0
    • D Offline
      D Offline
      DerManu
      wrote on last edited by
      #2

      Any top-level widget in Qt automatically gets a frame by the window manager and is floating. You create top-level widgets by not providing a parent to the ctor.

      So what you could do is subclass QWidget, reimplement the paintEvent and do your drawing there (with a QPainter painter(this); instance). Then in your main function, create a QApplication, create an instance of your widget-subclass without parent, and exec() the application.

      You could equally do this with a QMainWindow for example. (A subclass of this type is the default top-level widget if you're using QtCreator IDE)

      Further, I recommend any Qt tutorials and examples, abundant on the net.

      1 Reply Last reply
      0
      • T Offline
        T Offline
        tucnak
        wrote on last edited by
        #3

        Hi, ~TND5!

        Welcome to Qt Developer Network!

        As DerMan explained - any Qt top-level widgets without parents displays with QFrame. So if you run this code:
        @
        QWidget a; // a - without parent
        a.setGeometry(10,10,300,200); // x = 10, y = 10, width = 300, height = 200
        a.show(); // show it
        @

        It will show new window which will contain all that a contains. But if you run this code in MainWindow for e.g:
        @
        QWidget a(this); // a - with parent
        a.show(); // show it
        @

        It will display a in this widget.

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved