Making a window with Qt



  • 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!



  • 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.



  • 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.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.