layouts doesn't work in android



  • i've written a small program to test qt in android it has only 4 buttons, all of them just close application

    .h

    #ifndef TESTANDROID_H
    #define TESTANDROID_H
    
    #include <QMainWindow>
    #include <QHBoxLayout>
    #include <QPushButton>
    
    namespace Ui {
    class testandroid;
    }
    
    class testandroid : public QMainWindow
    {
        Q_OBJECT
    
    public:
        explicit testandroid(QWidget *parent = 0);
        ~testandroid();
    
    private slots:
        void button_click();
    
    private:
        QHBoxLayout *v_layout1;
        QHBoxLayout *v_layout2;
        QVBoxLayout *h_layout;
    
        QPushButton *button1;
        QPushButton *button2;
        QPushButton *button3;
        QPushButton *button4;
    
        Ui::testandroid *ui;
    };
    
    #endif // TESTANDROID_H
    

    .c

    #include "testandroid.h"
    #include "ui_testandroid.h"
    
    testandroid::testandroid(QWidget *parent) : QMainWindow(parent)//, ui(new Ui::testandroid)
    {
        //ui->setupUi(this);
    
        button1 = new QPushButton(tr("1"), this);
        //button1 = new QPushButton;
        //button1->setText("1");
        button2 = new QPushButton(tr("2"), this);
        button3 = new QPushButton(tr("3"), this);
        button4 = new QPushButton(tr("4"), this);
        //button4 = new QPushButton;
        //button4->setText("4");
        connect(button1, SIGNAL(clicked()), this, SLOT(button_click()));
        connect(button2, SIGNAL(clicked()), this, SLOT(button_click()));
        connect(button3, SIGNAL(clicked()), this, SLOT(button_click()));
        connect(button4, SIGNAL(clicked()), this, SLOT(button_click()));
    
        //h_layout1 = new QHBoxLayout(this);
        v_layout1 = new QHBoxLayout;
        v_layout2 = new QHBoxLayout;
        h_layout = new QVBoxLayout;
    
        v_layout1->addWidget(button1);
        v_layout1->addWidget(button2);
        v_layout2->addWidget(button3);
        v_layout2->addWidget(button4);
        h_layout->addLayout(v_layout1);
        h_layout->addLayout(v_layout2);
        this->setLayout(h_layout);
    }
    
    testandroid::~testandroid()
    {
        //delete ui;
    }
    
    void testandroid::button_click()
    {
        exit(0);
    }
    
    void testandroid::on_pushButton_clicked()
    {
        exit(0);
    }
    
    void testandroid::on_pushButton_4_clicked()
    {
        exit(0);
    }
    

    and here is what i see on emulator
    0_1515760133182_3_layouts.png
    and on my phone (Sony M2 Dual, Android 5.1)
    1_1515760133182_Screenshot_2018-01-12-15-28-46.png


  • Moderators

    @DrageFabeldyr
    since you are using a QMainWindow you are not supposed to call setLayout() on it.
    You should rather add a content widget (with your layout) via setCentralWidget() to avoid interfering with QMainWindow's default layout like you encountered.



  • @raven-worx
    thank you a lot, i was sure i'm using QWidget, but in fact i used QMainWindow and didn't see it
    i changed it to QWidget and now it works correctly


Log in to reply
 

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