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. Using code instead of the editor.
Forum Updated to NodeBB v4.3 + New Features

Using code instead of the editor.

Scheduled Pinned Locked Moved General and Desktop
6 Posts 4 Posters 1.2k Views 3 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.
  • S Offline
    S Offline
    Sunnykins
    wrote on last edited by
    #1

    This is probably very basic but i'm new to Qt and i can't seem to find info on how to do some things in code instead of the editor.

    I want to add a QScrollArea to the main window, and set the windows layout to QHBoxLayout. I can do it in the editor but when i try do it in code the scroll area just appears as a small box in the top left corner over the the top of the menu bar and doesn't scale with the window or anything.

    Here's the code i tried.
    in MainWindow.h

        QHBoxLayout* m_layout;
        QScrollArea* m_scrollArea;
    

    in MainWindow.cpp

        m_layout = new QHBoxLayout(this);
        m_scrollArea = new QScrollArea(this);
        this->layout()->addWidget(m_scrollArea);
    

    Again sorry if this is basic stuff I just can't find what i'm suppose to do.

    p3c0P 1 Reply Last reply
    0
    • dheerendraD Offline
      dheerendraD Offline
      dheerendra
      Qt Champions 2022
      wrote on last edited by
      #2

      what is the this object ? Is it QMainWindow or QWidget ? My guess is that it must be QMainWindow. When you run the program, it must be giving warning saying that "Not able to set the layout". Please check that.

      Dheerendra
      @Community Service
      Certified Qt Specialist
      http://www.pthinks.com

      1 Reply Last reply
      4
      • S Sunnykins

        This is probably very basic but i'm new to Qt and i can't seem to find info on how to do some things in code instead of the editor.

        I want to add a QScrollArea to the main window, and set the windows layout to QHBoxLayout. I can do it in the editor but when i try do it in code the scroll area just appears as a small box in the top left corner over the the top of the menu bar and doesn't scale with the window or anything.

        Here's the code i tried.
        in MainWindow.h

            QHBoxLayout* m_layout;
            QScrollArea* m_scrollArea;
        

        in MainWindow.cpp

            m_layout = new QHBoxLayout(this);
            m_scrollArea = new QScrollArea(this);
            this->layout()->addWidget(m_scrollArea);
        

        Again sorry if this is basic stuff I just can't find what i'm suppose to do.

        p3c0P Offline
        p3c0P Offline
        p3c0
        Moderators
        wrote on last edited by
        #3

        Hi @Sunnykins,
        Since you are creating your own layout using QHBoxLayout you should add the widget to the layout you created. layout() returns the already set layout to mainwindow. Instead set your newly created layout using setLayout. Something like this:

        m_layout = new QHBoxLayout;
        m_scrollArea = new QScrollArea(this);
        m_layout->addWidget(m_scrollArea);
        ui->centralWidget->setLayout(m_layout); //set layout to MainWindow's central widget
        

        157

        1 Reply Last reply
        1
        • S Offline
          S Offline
          Sunnykins
          wrote on last edited by Sunnykins
          #4

          ui->centralWidget->setLayout(m_layout);

          Thanks p3c0, that fixed it. I did try:

          this->setLayout(m_layout);
          

          but nothing seemed to happen.
          Yeah dheerendra i was getting that warning. I'm guess the window itself and the ui are different things? do i always go through the ui pointer when i add a widget etc?

          Also since the layout doesn't have a parent does that mean i have to explicitly delete it since it won't be deleted when it's parent is?

          edit: actually does the layout need to be a pointer at all? guess i could just make it an object.

          p3c0P 1 Reply Last reply
          0
          • M Offline
            M Offline
            mcosta
            wrote on last edited by
            #5

            Hi and welcome to devnet,

            the solution is

            m_layout->addWidget(m_scrollArea);
            this->setLayout(m_layout);
            

            Once your problem is solved don't forget to:

            • Mark the thread as SOLVED using the Topic Tool menu
            • Vote up the answer(s) that helped you to solve the issue

            You can embed images using (http://imgur.com/) or (http://postimage.org/)

            1 Reply Last reply
            0
            • S Sunnykins

              ui->centralWidget->setLayout(m_layout);

              Thanks p3c0, that fixed it. I did try:

              this->setLayout(m_layout);
              

              but nothing seemed to happen.
              Yeah dheerendra i was getting that warning. I'm guess the window itself and the ui are different things? do i always go through the ui pointer when i add a widget etc?

              Also since the layout doesn't have a parent does that mean i have to explicitly delete it since it won't be deleted when it's parent is?

              edit: actually does the layout need to be a pointer at all? guess i could just make it an object.

              p3c0P Offline
              p3c0P Offline
              p3c0
              Moderators
              wrote on last edited by
              #6

              @Sunnykins said:

              ui->centralWidget->setLayout(m_layout);

              Thanks p3c0, that fixed it. I did try:

              this->setLayout(m_layout);
              

              but nothing seemed to happen.
              Yeah dheerendra i was getting that warning. I'm guess the window itself and the ui are different things? do i always go through the ui pointer when i add a widget etc?

              Also since the layout doesn't have a parent does that mean i have to explicitly delete it since it won't be deleted when it's parent is?

              setLayout here would set layout for the mainwindow which fails because it already has a layout set for central widget.

              157

              1 Reply Last reply
              1

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved