Using unity menu bar in Qt - c++

I have a simple Qt MainWindow:
// ui/mainwindow.hpp
class MainWindow: public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget* parent = 0);
public slots:
private:
};
// ui/mainwindow.cpp
MainWindow::MainWindow(QWidget* parent): QMainWindow(parent)
{
QMenu* menuFile = menuBar()->addMenu(tr("File"));
menuFile->addAction(tr("Some"));
}
// Application entry
int main(int argc, char* argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
I'm noticing that with Ubuntu and Unity, the menu bar is not showing on the top of the screen but instead inside the application's window.
How do I make the menu bar show up on the top of the screen?

Which version of Qt are you using?
For Qt 4.8, the package appmenu-qt needs to be installed.
For Qt 5.2+, the package appmenu-qt5 needs to be installed and QT_QPA_PLATFORMTHEME=appmenu-qt5` being set in the environment
This is not really something that you influence on the app developer's side.

Related

How can I create a Qt project without a UI/Designer file?

I'd like my project to be written programmatically instead of using Qt Designer.
EDIT:
I'm just asking for a template :P
This requirement has a very simple solution: Just do write your code programatically instead of using QtDesigner. :)
EDIT 2: I found out how, feel free to use the code down below:
#include <QtWidgets/QDialog>
#include <QtWidgets/QApplication>
class MyWindow : public QDialog {
public:
MyWindow(QWidget* parent = nullptr);
};
MyWindow::MyWindow(QWidget* parent)
: QDialog(parent)
{
setWindowTitle("MyWindow");
// Make widgets, etc...
}
int main(int argc, char** argv)
{
QApplication app(argc, argv);
auto win = new MyWindow;
win->show();
return app.exec();
}

Enable resizing on QWidget

I want a resize feature in a QWidget using Qt, like the one shown in the image below.
I have used following tried following ways:
using QSizeGrip, setSizeGripEnabled
For completeness I'm showing two examples: with and without the Qt Designer.
Example using Qt Designer
Check the sizeGripEnabled property:
Preview from within the Qt Designer (Form > Preview...):
Minimal application to show the dialog:
#include <QtWidgets/QApplication>
#include <QDialog>
#include "ui_DialogButtonBottom.h"
class Dialog : public QDialog {
public:
Dialog(QWidget* parent = nullptr) :
QDialog(parent) {
ui.setupUi(this);
}
private:
Ui::Dialog ui;
};
int main(int argc, char* argv[])
{
QApplication a(argc, argv);
Dialog dlg;
return dlg.exec();
}
Resut
Without Qt Designer
#include <QtWidgets/QApplication>
#include <QDialog>
class Dialog : public QDialog {
public:
Dialog(QWidget* parent = nullptr) :
QDialog(parent) {
setWindowTitle("Example");
setSizeGripEnabled(true);
}
};
int main(int argc, char* argv[])
{
QApplication a(argc, argv);
Dialog dlg;
return dlg.exec();
}
Result
Update to include Frameless mode
Adding the Frameless windows hint doesn't change anything: it works correctly. Obviously, there is no frame so resize/move methods provided by the windows manager are not available.
#include <QtWidgets/QApplication>
#include <QDialog>
class Dialog : public QDialog {
public:
Dialog(QWidget* parent = nullptr, Qt::WindowFlags flags = 0) :
QDialog(parent, flags) {
setWindowTitle("Example");
setSizeGripEnabled(true);
}
};
int main(int argc, char* argv[])
{
QApplication a(argc, argv);
Dialog dlg(nullptr, Qt::FramelessWindowHint); // frameless
return dlg.exec();
}
Result
As all the options are working straightforwardly, I'd suggest you to carefully review your code/UI design for things like setting a maximum/minimum size (if both are the same, the grip will still be available but won't change the size at all).

QPushButton not clickable in MainWindow

I'm writing a QT project in Xcode, I made a Widget application in the QT Editor and used the "qmake -spec macx-xcode" to convert the project into an Xcode project.
I have a standard project:
main.cpp
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
MainWindow w;
w.show();
return app.exec();
}
main window.cpp
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow)
{
m_button = new QPushButton(this);
m_button -> setText("button");
m_button->setGeometry(QRect(QPoint(100, 100),QSize(200, 50)));
QPushButton *workingButton = new QPushButton("Hello");
workingButton -> show();
connect(m_button, SIGNAL(clicked()), this, SLOT(quitButton()));
ui->setupUi(this);
}
void MainWindow::quitButton() {
m_button->setText("Example");
}
MainWindow::~MainWindow()
{
delete ui;
}
main window.h
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private slots:
void quitButton();
private:
Ui::MainWindow *ui;
QPushButton *m_button;
};
#endif
The m_button shows up in the mainWindow but it is not clickable but the workingButton, shows up in its own separate window, and in the connect, when I replace the m_button with the workingButton, it is able to call the function. Any idea why the m_button is not sending a signal or function not being called?
The reason is quite simple: you have other transparent widgets overlaid on top of m_button. You must ensure that the button is not covered by anything else. E.g. move the creation of the button after the setupUi call or make the button a child of the central widget. Generally speaking, the setupUi call should be the first thing in a widget's constructor.
You also don't need to dynamically allocate the child widgets: prefer holding things by value: less things can go wrong then, and you're having less overhead, too!
Thus, pretending that the Ui_MainWindow class was really generated by uic:
// https://github.com/KubaO/stackoverflown/tree/master/questions/simple-button-main-41729401
#include <QtWidgets>
class Ui_MainWindow {
public:
QWidget *central;
QGridLayout *layout;
QLabel *label;
void setupUi(QMainWindow *parent);
};
class MainWindow : public QMainWindow, private Ui_MainWindow {
Q_OBJECT
QPushButton m_button{"Click Me"};
public:
MainWindow(QWidget *parent = nullptr) : QMainWindow(parent) {
setupUi(this);
m_button.setParent(centralWidget());
m_button.setGeometry({{50, 50}, m_button.sizeHint()});
}
};
void Ui_MainWindow::setupUi(QMainWindow *parent) {
central = new QWidget{parent};
layout = new QGridLayout{central};
label = new QLabel{"Hello"};
label->setAlignment(Qt::AlignCenter);
label->setStyleSheet("background-color:blue; color:white;");
layout->addWidget(label, 0, 0);
parent->setCentralWidget(central);
parent->setMinimumSize(200, 200);
}
int main(int argc, char ** argv) {
QApplication app{argc, argv};
MainWindow w;
w.show();
return app.exec();
}
#include "main.moc"

Accessing Outside of a class

I am trying to access a QgraphicsView's Scene outside of a class. I can normally do this when I create a class that is derived from QGraohicsView, but this class is the MainWindow which derives from QMainWIndow and I can have it extend QGraphicsView because there is a conflict when you call the .show() method as the compiler does not know which one to choose.
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{}
So I create a new instance of QgraphicsView and make it public and also Qgraphicsscene and make it public.
Then in the exterior class
extern MainWindow * mainwindow
But when I try to access it and I get a undefined reference error.
mainwindow->view->scene()->addItem(item); or
mainwindow->mainwindow.scene->addItem(item);
Neither of them work.
I know its breaking encapsulation but there is no other way around this in this particular case.
** What I am trying to do is access a QgraphicsView's Scene outside of its class?
** MainWindow has public variables
QGraphicsScene *scene;
QGraphicsView * view;
In MainWindow.cpp
scene = new QGraphicsScene(this);
view = new QGraphicsView(scene);
view.show();
This works for me. Since view in your case was a pointer, you must access its members via ->, not .. But all that is superfluous anyway - you should store everything by value as much as possible and let the compiler worry about making sure the resources are freed when no longer needed. That's why you're using C++, not C, after all.
// https://github.com/KubaO/stackoverflown/tree/master/questions/simple-view-33508582
#include <QtWidgets>
class MainWindow : public QMainWindow {
Q_OBJECT
QGraphicsScene m_scene;
QWidget m_central;
QGraphicsView m_view; // must be declared after m_central per C++ semantics
QGridLayout m_layout;
public:
MainWindow(QWidget * parent = 0) :
QMainWindow(parent),
m_layout(&m_central) {
setCentralWidget(&m_central);
m_layout.addWidget(&m_view, 0, 0);
m_view.setScene(&m_scene);
}
QGraphicsScene * scene() { return &m_scene; }
QGraphicsView * view() { return &m_view; }
};
int main(int argc, char ** argv) {
QApplication app(argc, argv);
MainWindow win;
win.scene()->addEllipse(0, 0, 10, 10);
win.show();
return app.exec();
}
#include "main.moc"
You should also decide whether you need a QMainWindow at all. Just because a Qt Creator template uses it, doesn't mean you should blindly use it too. If you're not using QMainWindow's docking area functionality, a QDialog would be a more sensible base class to use:
// https://github.com/KubaO/stackoverflown/tree/master/questions/simple-view-33508582
#include <QtWidgets>
class MainWindow : public QDialog {
Q_OBJECT
QGraphicsScene m_scene;
QGraphicsView m_view;
QGridLayout m_layout;
public:
MainWindow(QWidget * parent = 0) :
QDialog(parent),
m_layout(this) {
m_layout.addWidget(&m_view, 0, 0);
m_view.setScene(&m_scene);
}
QGraphicsScene * scene() { return &m_scene; }
QGraphicsView * view() { return &m_view; }
};
int main(int argc, char ** argv) {
QApplication app(argc, argv);
MainWindow win;
win.scene()->addEllipse(0, 0, 10, 10);
win.show();
return app.exec();
}
#include "main.moc"

Qt How to open QDialog when main application done fully to load all widgets

Hello all
I have main windows application and I like to popup dialog for settings when the application (the qMainWindow)
Is fully loaded ? I tried to just in the main window constructor:
SettingsDialog settingsDialog;
settingsDialog.exec();
but when I start my application I see the QDialog and the main window minimized in the background
what I need that my main windows will be in the background but not minimized and the QDialog in the middle blocking the main
windows until ok button is preset
Use QTimer::singleShot with zero time interval it will call specified slot from the event loop when constructor and show() have been completed. Here is an example:
#include <QtCore/QTimer>
#include <QtGui/QApplication>
#include <QtGui/QDialog>
#include <QtGui/QMainWindow>
class MW : public QMainWindow
{
Q_OBJECT
public:
MW();
private slots:
void showDialog();
};
MW::MW()
{
QTimer::singleShot(0, this, SLOT(showDialog()));
}
void MW::showDialog()
{
QDialog d;
d.setWindowTitle("dialog");
d.exec();
}
int main(int argc, char* argv[])
{
QApplication app(argc, argv);
MW mw;
mw.show();
app.exec();
}