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. Qt menubar disappears when I use scroll area
Forum Updated to NodeBB v4.3 + New Features

Qt menubar disappears when I use scroll area

Scheduled Pinned Locked Moved General and Desktop
4 Posts 2 Posters 1.6k 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.
  • S Offline
    S Offline
    Sai Kamat
    wrote on last edited by
    #1

    I want to implement a vertical scroll bar. I have implemented layouts for this(see the attached layouts.jpg), and also scroll area(see snippet in below code). Yet, when I run, the window does not have menubar. What am I missing here? I tried adding the menubar and submenus through Qt designer, as well as through code, but the window does not show the menubar.

    If i use scroll area snippet, i can use the scrollbar, but don't see menubar. However If i comment out the scroll area snippet, i can see the menubar, but lose the scroll bar. I need both of them. How to solve this?

    "Your text to link here...":http://i.stack.imgur.com/Nj9OG.jpg

    Here is the code:-
    mainwindow.h
    @#ifndef MAINWINDOW_H
    #define MAINWINDOW_H

    #include <QMainWindow>
    #include <QScrollArea>
    namespace Ui {
    class MainWindow;
    }

    class MainWindow : public QMainWindow
    {
    Q_OBJECT

    public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

    private:
    Ui::MainWindow *ui;

    };

    #endif // MAINWINDOW_H@

    mainwindow.cpp
    @#include "mainwindow.h"
    #include "ui_mainwindow.h"

    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    ui->setupUi(this);
    layout()->setMenuBar(menuBar());
    QMenu *viewMenu;
    viewMenu = menuBar()->addMenu("VIEW");
    QMenu *resolutionSubMenu = viewMenu->addMenu("Resolution");
    QAction *QVGA = resolutionSubMenu->addAction("QVGA");

    QImage image(":\\image.jpg");
    ui->referenceImageLabel->setPixmap(QPixmap::fromImage(image));
    ui->deltaImageLabel->setPixmap(QPixmap::fromImage(image));
    ui->reconstructedImageLabel->setPixmap(QPixmap::fromImage(image));
    ui->referenceImageLabel->setMinimumSize(480, 640);ui->referenceImageLabel->setMinimumSize(480, 640);ui->referenceImageLabel->setMinimumSize(480, 640);
    
    /*Scroll area snippet*/
    QScrollArea *p_ScrollArea = new QScrollArea(this);
    p_ScrollArea->setAutoFillBackground(true);
    p_ScrollArea->setMinimumSize(width(), height());
    p_ScrollArea->setWidget(ui->centralWidget);
    

    }

    MainWindow::~MainWindow()
    {
    delete ui;
    }@

    main.cpp
    @
    #include "mainwindow.h"
    #include <QApplication>

    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);
    MainWindow w;
    w.show();

    return a.exec&#40;&#41;;
    

    }@

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Why ?

      @layout()->setMenuBar(menuBar());@

      The menu bar is already put at the right place for you when you use a QMainWindow

      @
      QScrollArea *p_ScrollArea = new QScrollArea(this);
      p_ScrollArea->setAutoFillBackground(true);
      p_ScrollArea->setMinimumSize(width(), height());
      p_ScrollArea->setWidget(ui->centralWidget);
      @

      You scroll area is just floating.

      Since you already use Designer to create your UI, why don't you also setup the scroll area and menus there ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • S Offline
        S Offline
        Sai Kamat
        wrote on last edited by
        #3

        Hi SGaist,
        For the 1st question, when i added menubar through the designer, and run the program, the menubar was not there. so i added the line programmatically, but that did not help either.

        For the 2nd question, i do not know how to implement scroll bar for three qlabels through designer mode. If you have a tutorial where multiple qlables can be scrolled using a single vertical or horizontal scrollbar, through designer mode, and can share, i'd be most grateful.

        Also, what's a floating scroll area?

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          The first thing you should do is have a look at Qt's example and tutorials you should then be able to create your widget.

          It's floating in the sense that you didn't set it as central widget nor in a layout thus you have to handle it's size and position by hand.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          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