Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. layouts doesn't work in android

layouts doesn't work in android

Scheduled Pinned Locked Moved Solved Mobile and Embedded
3 Posts 2 Posters 719 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.
  • DrageFabeldyrD Offline
    DrageFabeldyrD Offline
    DrageFabeldyr
    wrote on last edited by
    #1

    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

    raven-worxR 1 Reply Last reply
    0
    • DrageFabeldyrD DrageFabeldyr

      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

      raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by
      #2

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

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      DrageFabeldyrD 1 Reply Last reply
      3
      • raven-worxR raven-worx

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

        DrageFabeldyrD Offline
        DrageFabeldyrD Offline
        DrageFabeldyr
        wrote on last edited by DrageFabeldyr
        #3

        @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

        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