problems with setting up easy window with widget coming from it's own class



  • Hey,
    I want to create a window, which has some GroupBoxes and stuff in different Labels and a custom widget in the middle.
    First issue: In the widget, I want to do some painting stuff, so I would like to put it in it's own class.
    How would you recommend me to implement this (there are many ways, I guess).
    Second: how can I let the program displaying this: (it is in the constructor of MainWindow)

    QLabel *testLabel = new QLabel("Test");
    QVBoxLayout *layout = new QVBoxLayout;
    layout->addWidget(testLabel);
    setLayout(layout);
    setWindowTitle("Test");
    

    When I run the program, there is just a white window. What am I missing?
    That this does not depend directly to the title is, because I tried it with the custom widget earlier, same result, so I tried to do it as little and easy as it's possible.
    (I am aware, that probably this is a veeery bad beginner question, but I can't find answers anywhere)
    Thanks!

    PS: If you would realize the program in a completely different way, feel free to mention how :)


  • Qt Champions 2016

    Hi
    First of all.
    QMainwindow is special. It has a central area called centralWidget that contains the
    widgets. It is because Docks/docking.

    You should add to the central widget and not the window itself
    Like
    centralWidget->setLayout(layout);

    • I want to do some painting stuff, so I would like to put it in it's own class.
      Good idea.
      Let me know if u have questions to this.

    Is there a reason you want to do this in code?
    You can do it all in Designer and to use your custom widget , you can just use promotion.
    http://www.informit.com/articles/article.aspx?p=1405227&seqNum=3


  • Moderators

    @Niagarer

    Probably best ist to start with some of the examples. Eventually you can start to modify/extend one and how you can achieve what you want.

    When starting a new application, you can create the inital application through one the templates of Qt creator. There are different philosopies, but for beginers I think it is always a good idea to use the designer within creator and add what you.



  • @mrjj
    Thanks for the answer!
    Ok, I am still having little problems with opening the window (nothing happends, just a crash after a few seconds) but I think this should not be too difficult to solve.
    Yes I want to do it in code, because later, I will add much functionality. The painting stuff should be something like a visual concept scriptor (how everyone creates a concept for the functionality of a program).
    I think it is better to write the desining stuff in code, because I don't want to reach the point where I want to add some functionality and I have to rewrite the program just because the Designer isn't giving me the possibility to do what I want. (Yep I have no Idea how this huge framework works and what it can do, so the best I can do is trying to let all options open). (If this is really not needed, please let me know.) And as I saw some examples, I thought, it does not seem very tricky to do this in code. The problem is, that I do not have that much understanding of the framework yet to figure out what I am really doing wrong when other things happen as in the examples when I copy single blocks of code and trying to build something similar. Just watched a toturial series and a 6h crashcours on Udemy.

    The link seems to be helpful, thanks I will take a closer look tomorrow.


  • Qt Champions 2016

    @Niagarer
    Hi
    You can use both ways with no issues.
    When you design something in Designer. C++ is generated. ( look in setupUI() in mainwindow)
    So you can design and take the code and use directly. (and not the design)
    Or just use the Design.
    Anything you add in Designer is available via
    ui->
    so its not that using code prevents using Designer or reverse.

    So often you have tons of visual designs and then some code at start up to setup stuff / add items.

    But doing it in code , does give better feel of the framework :)



  • @mrjj
    Ok, I just set up the classes and I now am at the point, where I have to create interactions.
    I read a bit in the article you sent me, but unfortunately I am not that good in qt to really understand what is written there. I will continue, but so far it is a bit confusing, because I do not really know how this whole thing works and thinks.
    What I am looking for now is a signal called clicked or something. I have to check, if I click with the right or with the left mouse button on the widget etc.
    I do not really know, how I can realize this in Qt.
    Any suggestions?
    After I have this, I want to start some paintings with QPainter in the widget.
    kind regards!


  • Moderators

    @Niagarer What widget is it where you want to detect mouse clicks? You probably will need to subclass QWidget if you want to paint and overload http://doc.qt.io/qt-5/qwidget.html#mousePressEvent



  • @jsulm
    It is just a normal Qt designer form class widget widget with QWidget as parent...
    Thank you for the link, it works fine!
    I don't know how I could subclass this that it works, but I will look.


  • Moderators

    @Niagarer You subclass as you do subclassing in C++.
    In QtCreator you then can use the "Promote to..." feature to "replace" a widget with your own, see http://doc.qt.io/qt-5.9/designer-using-custom-widgets.html



  • @jsulm
    Ok, thanks!
    I am currently trying to paint anything in the widget.
    The promotion is still a bit complicated to me (as you can see, my engl sucks a bit), I have not understood the concept yet.
    Maybe I will in future.
    Another question (but I think, it has something to do with it):
    I am trying to draw anything with QPainter to this widget.
    The problem is, that nothing comes, I just get this output:

    QWidget::paintEngine: Should no longer be called
    QPainter::begin: Paint device returned engine == 0, type: 1
    QPainter::setPen: Painter not active
    

    with this code in the constructor of my widget (copied from the docs):

        QPainter painter(this);
        painter.setPen(QPen(Qt::white, 12, Qt::SolidLine, Qt::RoundCap));
        painter.drawLine( 0, 0, 100, 100 );
    

    I know, that there is a already a topic about this ( https://forum.qt.io/topic/64693/unable-to-paint-on-qt-widget-shows-error-paintengine-should-no-longer-be-called )
    But yet I was not able to see what I am doing wrong, because the topic is about handeling with a picture.
    regards Niagarer


  • Moderators

    @Niagarer In the thread you linked there is the explanation:
    "You are calling drawFirstPoint and drawSecondPoint from within mousePressEvent. Don't do that. The only place you should create and use a painter is inside paintEvent."

    You should not draw inside pressEvent. Instead store needed information (like cursor position, pressed or not pressed) and use this information inside paintEvent.

    See
    " Chris Kawa Moderators 29 Feb 2016, 22:05

    You are calling drawFirstPoint and drawSecondPoint from within mousePressEvent. Don't do that. The only place you should create and use a painter is inside paintEvent. Instead of drawing from mouse event store the data to draw (the position and which button was pressed) and call update(). This will schedule a paint event, inside of which you can check the data and paint what's needed.

    Btw:

    void paintEvent(QPaintEvent *event);
    void mousePressEvent(QMouseEvent *event) Q_DECL_OVERRIDE;

    Be consistent. If you use Q_DECL_OVERRIDE on one override (good!), use it on all of them.
    "


Log in to reply
 

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