Results 1 to 2 of 2

Thread: Qt menubar disappears when I use scroll area

  1. #1
    Join Date
    Feb 2014
    Posts
    11
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default Qt menubar disappears when I use scroll area

    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?
    layouts.JPG

    Here is the code:- mainwindow.h
    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include <QMainWindow>
    5. #include <QScrollArea>
    6. namespace Ui {
    7. class MainWindow;
    8. }
    9.  
    10. class MainWindow : public QMainWindow
    11. {
    12. Q_OBJECT
    13.  
    14. public:
    15. explicit MainWindow(QWidget *parent = 0);
    16. ~MainWindow();
    17.  
    18. private:
    19. Ui::MainWindow *ui;
    20.  
    21.  
    22. };
    23.  
    24. #endif // MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 
    mainwindow.cpp
    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3.  
    4. MainWindow::MainWindow(QWidget *parent) :
    5. QMainWindow(parent),
    6. ui(new Ui::MainWindow)
    7. {
    8. ui->setupUi(this);
    9. layout()->setMenuBar(menuBar());
    10. QMenu *viewMenu;
    11. viewMenu = menuBar()->addMenu("VIEW");
    12. QMenu *resolutionSubMenu = viewMenu->addMenu("Resolution");
    13. QAction *QVGA = resolutionSubMenu->addAction("QVGA");
    14.  
    15.  
    16. QImage image(":\\image.jpg");
    17. ui->referenceImageLabel->setPixmap(QPixmap::fromImage(image));
    18. ui->deltaImageLabel->setPixmap(QPixmap::fromImage(image));
    19. ui->reconstructedImageLabel->setPixmap(QPixmap::fromImage(image));
    20. ui->referenceImageLabel->setMinimumSize(480, 640);ui->referenceImageLabel->setMinimumSize(480, 640);ui->referenceImageLabel->setMinimumSize(480, 640);
    21.  
    22. /*Scroll area snippet*/
    23. QScrollArea *p_ScrollArea = new QScrollArea(this);
    24. p_ScrollArea->setAutoFillBackground(true);
    25. p_ScrollArea->setMinimumSize(width(), height());
    26. p_ScrollArea->setWidget(ui->centralWidget);
    27.  
    28. }
    29.  
    30. MainWindow::~MainWindow()
    31. {
    32. delete ui;
    33. }
    To copy to clipboard, switch view to plain text mode 
    main.cpp

    Qt Code:
    1. #include "mainwindow.h"
    2. #include <QApplication>
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QApplication a(argc, argv);
    7. MainWindow w;
    8. w.show();
    9.  
    10. return a.exec();
    11. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,709
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Qt menubar disappears when I use scroll area

    You are placing your central widget in to the scorll area, instead parenting to it.
    Qt Code:
    1. QScrollArea *p_ScrollArea = new QScrollArea(centralWidget());
    2. p_ScrollArea->setAutoFillBackground(false);
    3. centralWidget()->setGeometry(0,0,500,500);
    4. p_ScrollArea->setGeometry(0,0,100,100);
    5. QLabel* pLabel = new QLabel("test");
    6. pLabel->setGeometry(0,0,1000,1000);
    7. p_ScrollArea->setWidget(pLabel);
    To copy to clipboard, switch view to plain text mode 

    Also, setting the minimum size will not do when you change the size of your window.
    You need to make sure your scoll areay adjust to the windows size.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

Similar Threads

  1. Scroll bars in scroll area not comming in scroll area
    By aurora in forum Qt Programming
    Replies: 3
    Last Post: 4th January 2012, 07:50
  2. Scroll bars in scroll area not comming
    By aurora in forum Qt Programming
    Replies: 3
    Last Post: 27th December 2011, 20:56
  3. Q Scroll Area that scrolls??
    By hakermania in forum Newbie
    Replies: 6
    Last Post: 24th August 2010, 20:48
  4. QGraphicsView scroll bars disappears
    By jano_alex_es in forum Qt Programming
    Replies: 4
    Last Post: 25th November 2009, 15:17
  5. abstract scroll area
    By moowy in forum Qt Programming
    Replies: 2
    Last Post: 2nd October 2006, 10:15

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.