Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Simple QHBoxLayout question



  • I'm poking around with the Text Edit example for Qt 5.15.2 and would like to add a QHBoxLayout.

    I'm new to Qt/C++ and I'm certain there's something simple I'm missing, but the QHBoxLayout documentation suggests that the layout requires a pointer while the Text Edit example uses a normal class. How should I go about adding a horizontal layout?

        // QHBoxLayout Example
        QWidget *window = new QWidget;
        QPushButton *button1 = new QPushButton("One");
        ...
    
        QHBoxLayout *layout = new QHBoxLayout(window);
        ...
        layout->addWidget(button1);
    
        window->show();
    
        // Text Edit example
        ...
        TextEdit mw;
        ...
        mw.show();
        ...
    


  • @duyjuwumu
    It's simple

    739ff884-596b-4793-a49b-723250b5ef84-image.png
    Just for your code

         QWidget *window = new QWidget;
          QPushButton *button1 = new QPushButton("One");
       QHBoxLayout *layout = new QHBoxLayout(window);
    
      QTextEdit *mw = new QTextEdit(window) ;
    // You wrote TextEdit there 
    layout->addWidget(button1);
    layout->addWidget(mw);
        window->show();
    
    

    More on same

    730a31d4-8626-4881-b39f-e51814b6db07-image.png

    Ignore the style of application, Rest it is working fine
    
        QPushButton *button = new QPushButton("Clicke me button",this);
        QPushButton *secondButton = new QPushButton("Second click me button",this);
        QTextEdit   *myEditBox = new QTextEdit(this);
    
        QHBoxLayout *horizontal = new QHBoxLayout(this);
        horizontal->addWidget(button);
        horizontal->addWidget(secondButton);
        horizontal->addWidget(myEditBox);
    

    Create buttons or anything you want (like textEdit)
    Then just add to the layout using

    addWidget(*widget)
    !!! Remember to include QHBoxLayout , QPushButton and QTextEdit in this case

    For Your case,
    You can just add the layout using

    widget->setLayout(layout);



  • Thank you so much, your response helped me come to the following solution:

    #include "textedit.h"
    
    #include <QApplication>
    #include <QCommandLineParser>
    #include <QCommandLineOption>
    #include <QHBoxLayout>
    #include <QPushButton>
    #include <QScreen>
    
    int main(int argc, char *argv[])
    {
        Q_INIT_RESOURCE(textedit);
    
        QApplication a(argc, argv);
        QCoreApplication::setOrganizationName("QtProject");
        QCoreApplication::setApplicationName("Rich Text");
        QCoreApplication::setApplicationVersion(QT_VERSION_STR);
        QCommandLineParser parser;
        parser.setApplicationDescription(QCoreApplication::applicationName());
        parser.addHelpOption();
        parser.addVersionOption();
        parser.addPositionalArgument("file", "The file to open.");
        parser.process(a);
    
        TextEdit mw;
    
        const QRect availableGeometry = mw.screen()->availableGeometry();
        mw.resize(availableGeometry.width() / 2, (availableGeometry.height() * 2) / 3);
        mw.move((availableGeometry.width() - mw.width()) / 2,
                (availableGeometry.height() - mw.height()) / 2);
    
        if (!mw.load(parser.positionalArguments().value(0, QLatin1String(":/example.html"))))
            mw.fileNew();
    
        QWidget *window = new QWidget;
        QHBoxLayout *layout = new QHBoxLayout(window);
        QPushButton *button = new QPushButton("Clicke me button");
    
        layout->addWidget(button);
        layout->addWidget(&mw);  // pass a reference to the TextEdit instance
    
        window->show();
        //mw.show();  Show the entire window widget, not just TextEdit
        return a.exec();
    }
    

    horizontal_box_text_edit.png



  • @duyjuwumu I am glad that it helped you.
    Mark it as solved


Log in to reply