How to add a Menubar to a QWidget? - c++

I am currently writing a C++ Application using the Qt framework, in which the 'main window' inherits from the QWidget class:
class Draughts : public QWidget
{
Q_OBJECT
public:
explicit Draughts(QWidget *parent = 0);
~Draughts();
private:
Ui::Draughts *ui;
};
And I attempted to add a simple menu bar to the application, using the following code:
Draughts::Draughts(QWidget *parent) :
QWidget(parent),
ui(new Ui::Draughts)
{
ui->setupUi(this);
QWidget *menuWidget = new QWidget;
QMenu *menuGame = new QMenu("Game");
menuGame->addAction("New");
menuGame->addAction("Exit");
QMenu *menuHelp = new QMenu("Help");
menuHelp->addAction("How to Play...");
menuHelp->addAction("About");
//Setup the Application Menu
QMenuBar mainMenu(this);
mainMenu.addMenu(menuGame);
mainMenu.addMenu(menuHelp);
}
Should I be using the QMainWindow class instead of the QWidget class for my application?

It would be easier to use QMainWindow, because it provides a convenient menuBar() method:
QMenuBar* mainMenu = this->menuBar();
But it is possible to add it to QWidget, just as any other widget. Just don't allocate it in the local scope, because it will be deleted after the function finishes. Instead, do it like with other widgets:
QMenuBar mainMenu = new QMenuBar(this);
You should also probably add a layout to your widget, and add the menu to the layout to have more control over where does it appear. You may find this tutorial useful.

Related

How to hide a temporary search bar?

I have a window that contains a browser. Up is a toolbar. In the bottom of the window is a search bar.
Search bar has a close button [x].
When the user clicks the close button I want the bar to disappear.
I want the bar only appear when user press CTRL + F. I tried to connect the close butoon with .hide() command, but application crashes. I need help.
.cpp
DocumentationWin::DocumentationWin (QWidget * parent){
docs = new QTextBrowser( this );
//Prepare toolbar
toolbar = new QToolBar( this );
//add stuff to toolbar
//Prepare footer bar
searchlabel = new QLabel(tr("Find in page:"),this);
resultslabel = new QLabel("",this);
searchinput = new QLineEdit();
findprev = new QToolButton(this);
findprev->setArrowType(Qt::UpArrow);
connect(findprev, SIGNAL(clicked()), this, SLOT (clickFindPrev()));
findnext = new QToolButton(this);
findnext->setArrowType(Qt::DownArrow);
connect(findnext, SIGNAL(clicked()), this, SLOT (clickFindNext()));
QStyle *style = qApp->style();
QIcon closeIcon = style->standardIcon(QStyle::SP_TitleBarCloseButton);
QPushButton *closeButton = new QPushButton(this);
closeButton->setIcon(closeIcon);
closeButton->setFlat(true);
connect(closeButton, SIGNAL(clicked()), this, SLOT (clickCloseFind()));
QWidget *bottom = new QWidget;
QHBoxLayout *footer = new QHBoxLayout();
casecheckbox = new QCheckBox(tr("Case sensitive"),this);
footer->setContentsMargins(5,5,5,5);
footer->addWidget(searchlabel);
footer->addSpacing(3);
footer->addWidget(searchinput);
footer->addWidget(findprev);
footer->addWidget(findnext);
footer->addSpacing(10);
footer->addWidget(casecheckbox);
footer->addSpacing(10);
footer->addWidget(resultslabel);
footer->addStretch(1);
footer->addWidget(closeButton);
bottom->setLayout(footer);
//Prepare main layout
layout = new QVBoxLayout;
layout->setContentsMargins(0,0,0,0);
layout->setSpacing(0);
layout->addWidget(toolbar);
layout->addWidget(docs);
layout->addWidget(bottom);
this->setLayout(layout);
this->show();
}
void DocumentationWin::clickCloseFind(){
bottom->hide();
}
.h
class DocumentationWin : public QDialog
{
Q_OBJECT
public:
DocumentationWin(QWidget * parent);
protected:
virtual void keyPressEvent(QKeyEvent *);
private slots:
void clickCloseFind();
private:
QVBoxLayout* layout;
QToolBar* toolbar;
QTextBrowser* docs;
QBoxLayout* footer;
QLabel *searchlabel;
QLabel *resultslabel;
QLineEdit *searchinput;
QToolButton *findprev;
QToolButton *findnext;
QCheckBox *casecheckbox;
QWidget *bottom;
QPushButton *closeButton;
};
Ahh, the classic case of local variables hiding the members. There have been quite a few identical questions on SO about this. This is wrong:
QWidget *bottom = new QWidget;
You want:
bottom = new QWidget;
You'll run into these problems always because you dynamically allocate all the widgets - that's completely unnecessary.
Suggestions:
Hold the child widgets and layouts by value, don't dynamically allocate them.
Don't pass a parent to widgets that are managed by a layout. Every widget that is laid out will be automatically parented.
Don't redundantly call setLayout. A QLayout takes the widget to lay its children on as a constructor argument.
QWidget::hide() is a slot.
Many widgets take the text as a constructor argument.
If you don't have any arguments to pass to the constructor in a new expression, you can drop the parentheses (but we try to avoid these anyway):
searchinput = new QLineEdit; // not QLineEdit();
Widgets shouldn't usually show() themselves upon construction. No Qt widget does that. It's up to the widget's user to do it.
C++ overloads a method call syntax with construction syntax. To differentiate the two, prefer uniform initialization (Type{arg0, arg1, ...}) over old syntax that used ().
Here's how your code can look when you're using C++11. This compiles with either Qt 4 or Qt 5. If you don't target Qt 4, you should be using the new connect syntax, though.
As you can see, there isn't a single explicit dynamic allocation - that's how quite a bit of C++11 code will look, when the used types are sane.
// https://github.com/KubaO/stackoverflown/tree/master/questions/find-hide-38082794
#include <QtGui>
#if QT_VERSION >= QT_VERSION_CHECK(5,0,0)
#include <QtWidgets>
#endif
class DocumentationWin : public QDialog
{
Q_OBJECT
public:
explicit DocumentationWin(QWidget * parent = 0);
private:
QVBoxLayout layout{this};
QToolBar toolbar;
QTextBrowser docs;
QWidget bottom;
QHBoxLayout footer{&bottom};
QLabel searchlabel{tr("Find in page:")};
QLabel resultslabel;
QLineEdit searchinput;
QToolButton findprev;
QToolButton findnext;
QCheckBox casecheckbox{tr("Case sensitive")};
QPushButton closeButton;
Q_SLOT void onFindPrev() {}
Q_SLOT void onFindNext() {}
};
DocumentationWin::DocumentationWin(QWidget * parent) : QDialog(parent) {
findprev.setArrowType(Qt::UpArrow);
connect(&findprev, SIGNAL(clicked()), this, SLOT(onFindPrev()));
findnext.setArrowType(Qt::DownArrow);
connect(&findnext, SIGNAL(clicked()), this, SLOT(onFindNext()));
auto style = qApp->style();
auto closeIcon = style->standardIcon(QStyle::SP_TitleBarCloseButton);
closeButton.setIcon(closeIcon);
closeButton.setFlat(true);
connect(&closeButton, SIGNAL(clicked(bool)), &bottom, SLOT(hide()));
footer.setContentsMargins(5,5,5,5);
footer.addWidget(&searchlabel);
footer.addSpacing(3);
footer.addWidget(&searchinput);
footer.addWidget(&findprev);
footer.addWidget(&findnext);
footer.addSpacing(10);
footer.addWidget(&casecheckbox);
footer.addSpacing(10);
footer.addWidget(&resultslabel);
footer.addStretch(1);
footer.addWidget(&closeButton);
layout.setContentsMargins(0,0,0,0);
layout.setSpacing(0);
layout.addWidget(&toolbar);
layout.addWidget(&docs);
layout.addWidget(&bottom);
}
int main(int argc, char ** argv) {
QApplication app{argc, argv};
DocumentationWin win;
win.show();
return app.exec();
}
#include "main.moc"

Adding widget to layout only works in constructor of custom class

I have a class MainWindow subclassing QMainWindow. As the central widget I have a QScrollArea with a QWidget inside, as a container for my own custom widgets. I set a QVBoxLayout as the layout for the QWidget container and pass this layout to the constructor of my custom class, which is supposed to dynamically create instances of my custom widget class and add them to the layout.
class MainWindow : public QMainWindow {
Q_OBJECT
public:
MainWindow(QWidget *parent = Q_NULLPTR);
virtual ~MainWindow ();
private:
QVBoxLayout mainLayout;
QScrollArea scrollArea;
QWidget container;
NotificationControl control;
};
MainWindow::MainWindow (QWidget *parent) :
QMainWindow(parent),
container(&scrollArea),
control(&mainLayout) {
container.setLayout(&mainLayout);
scrollArea.setWidget(&container);
setCentralWidget(&scrollArea);
}
Now in my NotificationControl class I have a method addNotification:
void NotificationControl::addNotification (Notification notif) {
qDebug() << "NotificationControl addNotification" << notif.get_app_name();
NotificationWidget* widget = new NotificationWidget(notif);
container->addWidget(widget);
}
When this method is called, I get the debug output, but nothing is added to the layout. But if I add this to the end of the constructor of NotificationControl...
NotificationWidget* notif = new NotificationWidget(Notification());
container->addWidget(notif);
...for some reason it works. I'm pretty sure the problem isn't the NotificationWidget class. For testing purposes I made some changes, it doesn't actually use the Notification object that is passed at all.
What could be the problem?
Edit: I just noticed that adding widgets later (not in constructor) makes the widgets added in the constructor disappear.
Any widget added as a child widget must be explicitly shown if the parent widget is already visible. Usually, the child widgets are added before the parent widget is shown, and they then become visible themselves, too. But if you add them later, you must make them explicitly visible.

Promoting QWidget in Qt creator, problems with constructor

I subclassed QGraphicsView:
class CustomGraphicsView : public QGraphicsView
{
public:
CustomGraphicsView(QWidget *parent = 0);
...
}
The constructor in the .cpp file is then implemented like this:
CustomGraphicsView::CustomGraphicsView(QWidget * parent):
QGraphicsView(parent)
{
}
Now I promoted a QGraphicsView Widget via a Qt creator to CustomGraphicsView. But when I want to connect to the promoted widget in the constructor of my ImageWindow class;
ImageWindow::ImageWindow(QWidget *parent) :
QDialog(parent),
ui(new Ui::ImageWindow)
{
ui->setupUi(this);
CustomGraphicsView * view = ui->graphicsView();
}
I get the error message:
term does not evaluate to a function taking 0 arguments.
I specified a default value for the constructor namely QWidget *parent = 0, and in ui_image_window.h an argument is set:
graphicsView = new CustomGraphicsView(ImageWindow);
So what could cause this error then?
This is because graphicsView is a member rather than a method, so you do not need the parentheses. Just access it like view = ui->graphicsView. This is the same for all Qt widgets in your generated UI class - they are just members, not methods.

Qt build menu for touch screen

i want to build a QT application for windows/android.
I have one mainwindow in which i have different buttons to call submenus, as you can see on the pictures. What do you think is the best implementation for that?
I thought about replacing the central widget
Using QStackwidget
Or open a new widget on the same position and size and close after.
What do you think? Do you have some favourite implementation or do i miss some important?
Edit
My Mainwindow constructor
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
m_stacked = new QStackedWidget;
HomeScreen *homescreen=new HomeScreen(m_stacked);
Control *manual=new Control(m_stacked);
m_stacked->addWidget(homescreen);
m_stacked->addWidget(manual);
this->setCentralWidget(m_stacked);
}
Implementation of every Screen
class HomeScreen : public QWidget
{
Q_OBJECT
public:
explicit HomeScreen(QStackedWidget* stack, QWidget *parent = 0);
~HomeScreen();
private slots:
void on_pushButton_clicked();
private:
Ui::HomeScreen *ui;
QStackedWidget *m_stacks;
};
HomeScreen::HomeScreen(QStackedWidget* stack,QWidget *parent) :
m_stacks(stack),QWidget(parent),
ui(new Ui::HomeScreen)
{
ui->setupUi(this);
}
HomeScreen::~HomeScreen()
{
delete ui;
}
void HomeScreen::on_pushButton_clicked()
{
m_stacks->setCurrentIndex(1);
}
What is your solution to change the current Widget, inside a widget of the QStackwidget? I used the solution above what do you think about it?
QStackedWidget was made right for that, I don't see any reason why you should incline to other options. You can always removeWidget(widget) and delete widget; if you want to free the memory of some rarely used menu.
Edit:
Your code is fine, there can be only a few enhancements made. You can create enum for your indexes in some separate header file.
Option 1:
If you use only QStackedWidget as a parent menu, you can adjust the constructor:
HomeScreen(QStackedWidget* parent) :
QWidget(parent),
ui(new Ui::HomeScreen)
{
ui->setupUi(this);
}
If you'd want to access QStackedWidget and change an index, you spare m_stacks pointer and use the parent: dynamic_cast<QStackedWidget*>(parent())->setCurrentIndex(1 /* or enum */);.
Option 2:
Leave index changing to the parent MainWindow. Each menu will have a request signal:
signals:
void goToMenuRequest(int index /* or enum */);
which you connect to m_stacked's setCurrentIndex in the constructor of MainWindow.
Not a big things, but It'll make coding easier.

Is there any standard way to create drop-down menu from QLineEdit without QCompleter?

Is there any standard way to create drop-down menu from QLineEdit without QCompleter? For example, using QMenu or creating own class. Or there are any other existing widgets?
Or maybe I should use QAbstractItemModel for QCompleter? I've thought about it, but I don't really understand this QAbstractItemModel. If you have experience about creating menu in this way, please also help me.
So I need a common type of drop-down menu: menu with lines, everyone of which includes icon (QPixmap) and text (QLabel) in itself. It's like in Opera or Chrome browser in address input line, like the right part of Apple Spotlight etc.
It's not possible with QMenu because it catch focus when showed and hides when loses focus. However, it's possible to use QListWidget (or any other regular widget) for this. I developed some working example for the proof of concept. It's default Qt Widget project with QMainWindow as main window. You need to add QLineEdit with name "lineEdit" into it and create slot for textChanged signa. Here's the code:
MainWindow.h:
class MainWindow : public QMainWindow {
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private slots:
void on_lineEdit_textChanged(const QString &arg1);
private:
Ui::MainWindow *ui;
QListWidget* list;
};
MainWindow.cpp:
#include "MainWindow.h"
#include "ui_MainWindow.h"
#include <QDebug>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow),
list(new QListWidget)
{
ui->setupUi(this);
list->setWindowFlags(Qt::WindowFlags(
Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint));
list->setAttribute(Qt::WA_ShowWithoutActivating);
}
MainWindow::~MainWindow() {
delete list;
delete ui;
}
void MainWindow::on_lineEdit_textChanged(const QString &arg1) {
if (ui->lineEdit->text().isEmpty()) {
list->hide();
return;
}
list->clear();
list->addItem(ui->lineEdit->text());
list->addItem(tr("Google: ") + ui->lineEdit->text());
list->move(ui->lineEdit->mapToGlobal(QPoint(0, ui->lineEdit->height())));
if (!list->isVisible()) list->show();
}
There are several problems: you should hide menu when line edit loses focus or user move window, you can't set focus on the menu using down arrow button from line edit, etc. But I believe all these issues can be solved easily.
From what you describe, you could try an editable QComboBox: It has its own model and view, its own completer, and can display icons.
QComboBox *comboBox = new QComboBox;
...
comboBox->setEditable(true);
// The completer popup isn't enabled by default
comboBox->completer()->setCompletionMode(QCompleter::PopupCompletion);
And since that QCompleter can display icons, I guess you can use a regular QLineEdit with a QCompleter and a model with icons. For the model, you can use a QStandardItemModel.