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. [Solved] QWidget children problem

[Solved] QWidget children problem

Scheduled Pinned Locked Moved General and Desktop
6 Posts 3 Posters 4.5k Views 1 Watching
  • 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.
  • J Offline
    J Offline
    just1602
    wrote on last edited by
    #1

    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 {

    Q_OBJECT

    public:
    FindWindow(QString & findContent);
    FindWindow(QString & findContent, QWidget * parent);
    ~FindWindow();

    private:
    /*

    • 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);

    };

    #endif
    @

    @#include "find_window.hpp"

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

    }

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

    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"));

    m_btnLayout->addWidget(m_btnSearch);
    m_btnLayout->addWidget(m_btnCancel);

    m_strLayout->addWidget(m_findLbl);
    m_strLayout->addWidget(m_strToFind);

    m_mainLayout->addLayout(m_strLayout);
    m_mainLayout->addLayout(m_btnLayout);

    this->setLayout(m_mainLayout);
    }

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

    this->close();
    }

    FindWindow::~FindWindow(){
    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

    @++
    Justin : )

    1 Reply Last reply
    0
    • A Offline
      A Offline
      andre
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0
      • G Offline
        G Offline
        goetz
        wrote on last edited by
        #3

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

        http://www.catb.org/~esr/faqs/smart-questions.html

        1 Reply Last reply
        0
        • J Offline
          J Offline
          just1602
          wrote on last edited by
          #4

          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 ?

          @++
          Justin : )

          1 Reply Last reply
          0
          • G Offline
            G Offline
            goetz
            wrote on last edited by
            #5

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

            http://www.catb.org/~esr/faqs/smart-questions.html

            1 Reply Last reply
            0
            • J Offline
              J Offline
              just1602
              wrote on last edited by
              #6

              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 to ConfigWindow::ConfigWindow()'
              collect2: ld returned 1 exit status
              make: *** [QNotepad] Error 1
              @

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

              Thank you

              @++
              Justin : )

              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