Qt World Summit: Submit your Presentation

[Solved] QWidget children problem

  • Hello,

    I am making a text editor. My main window is a QMainWindow and I have two child windows that are QWidget.

    When I call my child I set the mainWindow as parent the two window merge and it create a weird window with a lot of glitch. If I don't set parent, my child QWidget is show correctly as a separate window, but the layout isn't show.

    Here is the code of one of my child QWidget, if you see something wrong.

    @#ifndef FIND_WINDOW_HPP
    #define FIND_WINDOW_HPP


    • Qt include
      #include <QWidget>
      #include <QLabel>
      #include <QLineEdit>
      #include <QPushButton>
      #include <QHBoxLayout>
      #include <QVBoxLayout>
      #include <QString>

    class FindWindow : public QWidget {


    FindWindow(QString & findContent);
    FindWindow(QString & findContent, QWidget * parent);


    • Window element
      QVBoxLayout *m_mainLayout;
      QHBoxLayout *m_strLayout;
      QHBoxLayout *m_btnLayout;
      QLabel *m_findLbl;
      QLineEdit *m_strToFind;
      QPushButton *m_btnSearch;
      QPushButton *m_btnCancel;


    • Private method
      void initWindow();

    private slots:
    void returnSearch(QString & stringToFind);



    @#include "find_window.hpp"

    FindWindow::FindWindow(QString & findContent) : QWidget(){


    FindWindow::FindWindow(QString & findContent, QWidget * parent = 0) : QWidget(parent) {

    connect(m_btnCancel, SIGNAL(clicked()), this, SLOT(close()));

    void FindWindow::initWindow(){
    m_mainLayout = new QVBoxLayout();
    m_strLayout = new QHBoxLayout();
    m_btnLayout = new QHBoxLayout();

    m_findLbl = new QLabel(tr("Find : "));
    m_strToFind = new QLineEdit();
    m_btnSearch = new QPushButton(tr("Search"));
    m_btnCancel = new QPushButton(tr("Cancel"));





    void FindWindow::returnSearch(QString & stringToFind){
    stringToFind = m_strToFind->text();


    delete m_btnSearch;
    delete m_btnCancel;
    delete m_findLbl;
    delete m_strToFind;
    delete m_btnLayout;
    delete m_strLayout;
    delete m_mainLayout;

    It's my first real project in c++. I'm sure the code isn't perfect. I will fallow your comments to improve my code.

    Thanks in advance !!

    PS : Sorry for my english, I'm a french speaking person and try to do my best ^^

    Edit : Here is the complete code : https://github.com/just1602/QNotepad/tree/dev

  • For a QMainWindow, you need to set one 'central widget'. I see you are not doing that. If you need to put more than one widget, you need to embed those inside one widget in a layout. Setting that widget as the central widget of a QMainWindow takes care of the layout of that one widget. No layout is needed there.

  • The code on github does not contain the FindWindow class - are you sure you're actually using the pasted code? Manipulating the QMessageBox class like in the code is not supported.

    Your code above is ok sofar.

    Some notes:
    You can combine your two constructors:

    // in the header file
    FindWindow(QString &findContent, QWidget *parent = 0);

    // in the implemenation
    FindwWindow::FindWindow(QString &findContetn, QWidget parent)
    : QWidget(parent)
    // ....

    For using your form as a toplevel widget (popup), you should derive from [[Doc:QDialog]] instead of QWidget. Dialogs are toplevel by default, even if given a parent and you can use exec on that.

    For just getting a singel value from the user, you might consider using [[Doc:QInputDialog]].

  • I'll try to derived from QDialog instead of QWidget. For the constructor, I did it like the first time, but the compilatoin failed with this code, so I write two constructor.

    Thank you, everything is now working !! : )

    Which QMessageBox implementation isn't support ?

  • Which error message? You didn't paste any yet ;-)

  • If I don't put parent this as parent, I got that error message :
    main_window.o: In function MainWindow::openOptions()': main_window.cpp:(.text+0x35e2): undefined reference toConfigWindow::ConfigWindow()'
    collect2: ld returned 1 exit status
    make: *** [QNotepad] Error 1

    And when I put this as parent everything work pefectly. ;-)

    Thank you

Log in to reply