I have a hard time adding menu Bar, menus and sub menus to Qt QMainWindow programmatically.
The following code produces an error:
QWidget::setLayout: Attempting to set QLayout "" on QMainWindow "", which already has a layout
Notes :
*.The main window come out without any menu or Layout (Empty!)
#include <QApplication>
#include <QApplication>
#include<QSlider>
#include<QSpinBox>
#include<QHBoxLayout>
#include<QWidget>
#include "mainwindow.h"
#include<QMenuBar>
#include<QStatusBar>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QMenuBar *menu = new QMenuBar;
QMenu *file = new QMenu();
file->addMenu("&File");
menu->addMenu(file);
QSlider *s1 = new QSlider(Qt::Horizontal);
QSlider *s2 = new QSlider(Qt::Vertical);
QSpinBox *sb = new QSpinBox;
QHBoxLayout *L = new QHBoxLayout;
L->addWidget(s1);
L->addWidget(s2);
L->addWidget(sb);
QMainWindow *w = new QMainWindow;
w->setLayout(L);
w->show();
return a.exec();
}
Each QMainWindow should have a central widget:
QMainWindow *w = new QMainWindow;
QWidget* centralWidget = new QWidget;
w->setCentralWidget( centralWidget );
centralWidget->setLayout(L);
w->show();
add layout to central widget:
#include <QApplication>
#include <QApplication>
#include<QSlider>
#include<QSpinBox>
#include<QHBoxLayout>
#include<QWidget>
#include<QMenuBar>
#include<QStatusBar>
#include <QMainWindow>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QMainWindow *w = new QMainWindow;
QMenuBar *menu = new QMenuBar;
QMenu *file = new QMenu();
file->addMenu("&File");
menu->addMenu(file);
QWidget *centralwidget = new QWidget(w);
w->setCentralWidget(centralwidget);
QSlider *s1 = new QSlider(Qt::Horizontal, centralwidget);
QSlider *s2 = new QSlider(Qt::Vertical, centralwidget);
QSpinBox *sb = new QSpinBox;
QHBoxLayout *L = new QHBoxLayout(centralwidget);
L->addWidget(s1);
L->addWidget(s2);
L->addWidget(sb);
w->show();
return a.exec();
}
This is the final version
#include <QApplication>
#include <QApplication>
#include<QSlider>
#include<QSpinBox>
#include<QHBoxLayout>
#include<QWidget>
#include "mainwindow.h"
#include<QMenuBar>
#include<QStatusBar>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QSlider *s1 = new QSlider(Qt::Horizontal);
QSlider *s2 = new QSlider(Qt::Vertical);
QSpinBox *sb = new QSpinBox;
QMainWindow *w = new QMainWindow;
QWidget *cw = new QWidget(w);
QMenuBar *menu = new QMenuBar(cw);
QHBoxLayout *L = new QHBoxLayout(cw);
L->addWidget(s1);
L->addWidget(s2);
L->addWidget(sb);
QMenu *file = new QMenu("&File");
file->addMenu("Open");
file->addMenu("new");
QMenu *Build = new QMenu("&Build");
Build->addAction("Rebuild this file");
Build->addAction("Rebuild All");
menu->addMenu(file);
menu->addMenu(Build);
w->setCentralWidget(cw);
w->show();
QObject::connect (s1,SIGNAL(valueChanged(int) ), sb,SLOT(setValue(int) ) );
QObject::connect (s1,SIGNAL(valueChanged(int) ), s2,SLOT(setValue(int) ) );
QObject::connect (s2,SIGNAL(valueChanged(int) ), sb,SLOT(setValue(int) ) );
QObject::connect (s2,SIGNAL(valueChanged(int) ), s1,SLOT(setValue(int) ) );
QObject::connect (sb,SIGNAL(valueChanged(int) ), s1,SLOT(setValue(int) ) );
QObject::connect (sb,SIGNAL(valueChanged(int) ), s2,SLOT(setValue(int) ) );
return a.exec();
}
To try to clearly answer the question suggested by the title (which is not a question by itself), QMainWindow does already have an empty menu bar by default besides other things like a central widget.
To access this QMenuBar and populate it with things of your choice, just call menuBar() from your QMainWindow instance.
To add a submenu to a QMenuBar, use QMenuBar::addMenu.
For example :
QAction* newAct = new QAction("save");
auto fileMenu = menuBar()->addMenu(tr("&File"));
fileMenu->addAction(newAct);
auto submenu = fileMenu->addMenu("Submenu");
submenu->addAction(new QAction("action1");
submenu->addAction(new QAction("action2");
For more information, look at this Qt example : https://doc.qt.io/qt-5/qtwidgets-mainwindows-menus-example.html and also the QMenuBar reference https://doc.qt.io/qt-5/qmenubar.html#details
I realize this is an ancient post , but the answers are unnecessary complicated
basic Qt Widget application
QApplication a(argc, argv);
MainWindow w;
w.menuBar()->addAction("TEST");
w.show();
plus
addAction("TEST");
has 4 overloads to actually implement the menu option
Related
I try to create a window in another window with Qt and delete the first window. When the second window is opened the first window is deleted but when I try to delete the second window I get this error:
HEAP[Flash-TestBench-Client-Rev1-0.exe]:
Heap block at 18FBC298 modified at 18FBC2C4 past requested size of 24
Here is my simplified code:
main.cpp
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
ConnectionWindow *w = new ConnectionWindow;
return a.exec();
}
connectionwindow.cpp
ConnectionWindow::ConnectionWindow(QWidget *parent)
: QMainWindow(parent)
{
QWidget *w = new QWidget;
QVBoxLayout *l = new QVBoxLayout;
QPushButton *button = new QPushButton;
button->setText("Connect");
conenect(button, SINGNAL(pressed()), this, (openW()));
l->addWidget(button);
w->setLayout(l);
this->setCentralWidget(w);
this->setAttribute(Qt::WA_DeleteOnClose);
this->show;
}
void ConnectionWindow::openW(){
MainWindow *window = new MainWindow;
this->close;
}
mainwindow.cpp
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
QWidget *w = new QWidget;
QVBoxLayout *l = new QVBoxLayout;
QPushButton *button = new QPushButton;
button->setText("Close");
conenect(button, SINGNAL(pressed()), this, (closeW));
l->addWidget(button);
w->setLayout(l);
this->setCentralWidget(w);
this->setAttribute(Qt::WA_DeleteOnClose);
this->show;
}
void MainWindow::closeW(){
this->close();
}
I have a custom widget with some standard child widgets inside. If I make a separate test project and redefine my custom widget to inherit QMainWindow, everything is fine. However, if my custom widget inherits QWidget, the window opens, but there are no child widgets inside.
This is the code:
controls.h:
#include <QtGui>
#include <QVBoxLayout>
#include <QLineEdit>
#include <QPushButton>
class Controls : public QWidget
{
Q_OBJECT
public:
Controls();
private slots:
void render();
private:
QWidget *frame;
QWidget *renderFrame;
QVBoxLayout *layout;
QLineEdit *rayleigh;
QLineEdit *mie;
QLineEdit *angle;
QPushButton *renderButton;
};
controls.cpp:
#include "controls.h"
Controls::Controls()
{
frame = new QWidget;
layout = new QVBoxLayout(frame);
rayleigh = new QLineEdit;
mie = new QLineEdit;
angle = new QLineEdit;
renderButton = new QPushButton(tr("Render"));
layout->addWidget(rayleigh);
layout->addWidget(mie);
layout->addWidget(angle);
layout->addWidget(renderButton);
frame->setLayout(layout);
setFixedSize(200, 400);
connect(renderButton, SIGNAL(clicked()), this, SLOT(render()));
}
main.cpp:
#include <QApplication>
#include "controls.h"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
Controls *controls = new Controls();
controls->show();
return app.exec();
}
This opens up a window with correct dimensions, but with no content inside.
Bear in mind this is my first day using Qt. I need to make this work without inheriting QMainWindow because later on I need to put this on a QMainWindow.
You're missing a top level layout:
Controls::Controls()
{
... (yoour code)
QVBoxLayout* topLevel = new QVBoxLayout(this);
topLevel->addWidget( frame );
}
Or, if frame is not used anywhere else, directly:
Controls::Controls()
{
layout = new QVBoxLayout(this);
rayleigh = new QLineEdit;
mie = new QLineEdit;
angle = new QLineEdit;
renderButton = new QPushButton(tr("Render"));
layout->addWidget(rayleigh);
layout->addWidget(mie);
layout->addWidget(angle);
layout->addWidget(renderButton);
setFixedSize(200, 400);
connect(renderButton, SIGNAL(clicked()), this, SLOT(render()));
}
Note that setLayout is done automatically when QLayout is created (using parent widget)
You'll want to set a layout on your Controls class for managing its child sizes. I'd recommend removing your frame widget.
controls.cpp
Controls::Controls()
{
layout = new QVBoxLayout(this);
.
.
.
}
main.cpp
int main(int argc, char* argv[])
{
QApplication app(argc, argv);
MainWindow w;
w.show();
return app.exec();
}
I'm building a simple application, the main window have to shows two widgets (QTreeView on the right, QTabWidget on the left), to perform this i use a QHBoxLayout.
This is the code i've written(constructor of MainWindow):
MainWindow::MainWindow()
{
mainLayout = new QHBoxLayout(this);
tabber = new QTabWidget(this);
analysisTreeView = new QTreeView(this);
tabber->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
mainLayout->addWidget(tabber, 0);
mainLayout->addWidget(analysisTreeView, 0);
createActions();
createMenus();
createToolBars();
connect(tabber, SIGNAL(currentChanged(int)), this, SLOT(currentTabChanged(int)));
setLayout(mainLayout);
}
But when i run the application the main window shows no widgets. Why?
On request, i add some code:
Once a button in the mainwindows's toolbar is clicked a new tab is added to tabber:
void MainWindow::newSheet()
{
GraphicsScene *newScene = new GraphicsScene(itemMenu,this);
QGraphicsView *newView = new QGraphicsView(this);
newScene->setSceneRect(-200, -200, 400, 400);
newView->scale(1.5,1.5);
newView->setCacheMode(QGraphicsView::CacheBackground);
newView->setViewportUpdateMode(QGraphicsView::BoundingRectViewportUpdate);
newView->setRenderHint(QPainter::Antialiasing);
newView->setTransformationAnchor(QGraphicsView::AnchorUnderMouse);
newView->setScene(newScene);
sheetList.append(newView);
tabber->addTab(newView,"PNC");
connect(newScene, SIGNAL(itemInserted(PItem*)), this, SLOT(itemInserted(PItem*)));
connect(newScene, SIGNAL(requestUpdateGUI(GraphicsScene*)), this, SLOT(updateGUI(GraphicsScene*)));
}
My main.cpp:
#include <QApplication>
#include "mainwindow.h"
int main(int argc, char *argv[])
{
Q_INIT_RESOURCE(application);
QApplication a(argc, argv);
MainWindow window;
window.showMaximized();
return a.exec();
}
I suppose your class specializes QMainWindow. If so, it needs a centralWidget to be set:
MainWindow::MainWindow()
{
// added by jpo38
QWidget* mainWidget = new QWidget( this );
setCentralWidget( mainWidget );
// end added by jpo38
mainLayout = new QHBoxLayout(mainWidget);
tabber = new QTabWidget(mainWidget);
analysisTreeView = new QTreeView(mainWidget);
tabber->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
mainLayout->addWidget(tabber, 0);
mainLayout->addWidget(analysisTreeView, 0);
createActions();
createMenus();
createToolBars();
connect(tabber, SIGNAL(currentChanged(int)), this, SLOT(currentTabChanged(int)));
setLayout(mainLayout);
}
How do I create a simple widget that would contain a first, middle and last name field and add it to the main window class?
I ask because I cant figure out why is this simple widget attempt below is not working, what have I missed?
main
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
mainWindow class
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent){
QMainWindow *mainView = new QMainWindow;
setCentralWidget(mainView);
CardUI *card = new CardUI;
QHBoxLayout *hCard = new QHBoxLayout;
hCard->addWidget(card);
mainView->setLayout(hCard);
mainView->show();
}
cardui class
CardUI::CardUI(QWidget *parent) : QWidget(parent){
QLineEdit *fnText = new QLineEdit;
QLineEdit *miText = new QLineEdit;
QLineEdit *lnText = new QLineEdit;
QHBoxLayout *name = new QHBoxLayout;
name->addWidget(fnText);
name->addWidget(miText);
name->addWidget(lnText);
setLayout(name);
}
QMainWindow *mainView = new QMainWindow;
//....
mainView->setLayout(hCard);
You should not change layout of QMainWindow. Use setCentralWidget or add toolbars/docks using given API instead.
In this particular case you shouldn't create mainView as QMainWindow (you cannot have two main windows in one application, right?). You can change mainView type to QWidget, but you can even don't create any proxy widgets, and just
MainWindow::MainWindow(QWidget *parent); : QMainWindow(parent){
card = new CardUI;
setCentralWidget(card);
}
Greetings all,
Is there any widget to separate two QWidgets and also give full focus to a one widget.
As shown in following figure ?
Thanks in advance,
umanga
How about QSplitter?
QWidget 1, for exmaple, QListView. QWidget 2 is a combination of QWidgets (the left part is simple QPushButton with show/hide caption, and the right part another widget)... All you have to do, is to hide your QWidget2 when user clicked on QPushButton...
If you need an example, I may post it.
Updated
main.cpp
#include "splitter.h"
#include <QtGui/QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
splitter w;
w.show();
return a.exec();
}
splitter.h
#ifndef SPLITTER_H
#define SPLITTER_H
#include <QtGui/QDialog>
class splitter : public QDialog
{
Q_OBJECT;
QWidget* widget1;
QWidget* widget2;
QPushButton* button;
public:
splitter(QWidget *parent = 0, Qt::WFlags flags = 0);
~splitter();
private slots:
void showHide(void);
};
#endif // SPLITTER_H
splitter.cpp
#include <QtGui>
#include "splitter.h"
splitter::splitter(QWidget *parent, Qt::WFlags flags)
: QDialog(parent, flags)
{
QApplication::setStyle("plastique");
QListView* listView = new QListView;
QTableView* tableView = new QTableView;
button = new QPushButton("Hide >");
widget1 = new QWidget;
QHBoxLayout* w1Layout = new QHBoxLayout;
w1Layout->addWidget(listView);
w1Layout->addWidget(button);
widget1->setLayout(w1Layout);
widget2 = new QWidget;
QHBoxLayout* w2Layout = new QHBoxLayout;
w2Layout->addWidget(tableView);
widget2->setLayout(w2Layout);
QSplitter *mainSplitter = new QSplitter(this);
mainSplitter->addWidget(widget1);
mainSplitter->addWidget(widget2);
connect(button, SIGNAL(clicked()), this, SLOT(showHide()));
QVBoxLayout *mainLayout = new QVBoxLayout;
mainLayout->addWidget(mainSplitter);
setLayout(mainLayout);
}
splitter::~splitter()
{}
void splitter::showHide(void)
{
if (widget2->isVisible())
{ // hide
widget2->setVisible(false);
button->setText("< Show");
}
else
{ // show
widget2->setVisible(true);
button->setText("Hide >");
}
}