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
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QScrollArea>
namespace Ui {
class MainWindow;
}
{
Q_OBJECT
public:
explicit MainWindow
(QWidget *parent
= 0);
~MainWindow();
private:
Ui::MainWindow *ui;
};
#endif // 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
To copy to clipboard, switch view to plain text mode
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow
::MainWindow(QWidget *parent
) : ui(new Ui::MainWindow)
{
ui->setupUi(this);
layout()->setMenuBar(menuBar());
viewMenu = menuBar()->addMenu("VIEW");
QMenu *resolutionSubMenu
= viewMenu
->addMenu
("Resolution");
QAction *QVGA
= resolutionSubMenu
->addAction
("QVGA");
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*/
p_ScrollArea->setAutoFillBackground(true);
p_ScrollArea->setMinimumSize(width(), height());
p_ScrollArea->setWidget(ui->centralWidget);
}
MainWindow::~MainWindow()
{
delete ui;
}
#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;
}
To copy to clipboard, switch view to plain text mode
main.cpp
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
MainWindow w;
w.show();
return a.exec();
}
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
To copy to clipboard, switch view to plain text mode
Bookmarks