Add QWidgetAction to QMenu through qtcreator/qtdesigner - c++

I am wondering if there is a way to add QWidgetAction to QMainwindow->QMenuBar->QMenu using either qtcreator or qtdesigner.
I can add a widget through the code like that:
//ui->myMenu is QMenu in QMenuBar of QMainWindow
QWidgetAction *act = new QWidgetAction(ui->myMenu);
QLineEdit* edt = new QLineEdit("I am Line edit",ui->myMenu);
//setup edt ...
act->setDefaultWidget(edt);
ui->myMenu->addAction(act);
It compiles and works as expected.
However, I cannot achieve same behavior using ui designer - it only lets me add QAction and QMenu classes as parts of QMenuBar/QMenu, and does not give option to promote QAction to QWidgetAction. Is there a way to add QWidgetAction and a widget associated with it through designer to have them as part of Ui namespace and their properties editable through ui editor?

Unfortunately that's not possible.

Related

Qt generate UI based on template

I'm looking to generate a set of labels and buttons based on some kind of template, but I don't know how to do the template part.
I'll be using a tab widget which I already have set up, and in one the tabs, I want to have a two labels, a custom button, and a textbox. It'll be repeated around 40-50 times (dependent on a given value at startup) and have spacing as needed.
Once I have a template, I foresee calling it in a loop and setting the appropriate displayed text(Label_1, Label_2, etc) and connect statements where needed.
As I said, I don't know how to template parts of the UI so they can be placed in a kind of auto-generation.
I had thought of making one group, copying the xml, and somehow adding it but that doesn't seem to be a proper way. A little new to Qt.
This is roughly the layout I want to repeat. It has two labels, a lineedit, and one pushbutton.
There's no "good" way to do this in QtDesigner/QtCreator. At best you could copy/paste the set of controls 50 times and then in C++ code hide the ones you don't need. But I wouldn't recommend this.
Instead, just create the controls (labels/button/text box), and a layout to hold them, in C++ code, inside a loop which iterates however many times you need at runtime. Insert the controls layout into the tab widget page layout which you have set up in designer mode. It is not difficult, and will actually be more efficient than what QtDesigner produces since that tends to generate more code than you typically need in the first place.
As a starting point, you could look at the C++ code which is generated by the Qt UI Compiler (UIC) tool for your current design (it takes the XML from designer and turns it into C++ code). You can find this in the build folder for your project, typically named something like ui_ClassName.h, probably in a ui subfolder of the build tree.
UPDATE:
Another, possibly better, way to do this is to create the "template" QWidget class/form, which is going to be used multiple times, as a separate object. The "template" design could be created/maintained using QtCreator/Designer (or just directly in C++). The (possible) advantage here is that as the app requirements evolve, the template widget can be expanded with additional functionality or even re-used in other parts of the UI.
For example, I'd assume the text editor and button in the given mockup image will actually need to do something (eg. edit data and submit it). So some basic functionality can be built into the "template" widget, for example to emit a signal with the text contents of the line editor when the button is pressed.
I put together a quick example. I'm creating the simple MainWindow in pure C++ to simplify/shorten the example code. The "template" I'm calling an Editor. The Editor class and UI form I initially created with the QtCreator wizard (New -> Qt Designer Form Class). I then added the label/control widgets in designer mode. And in C++, a textEdited(const QString &text) signal in the header, and in the Editor() constructor a lambda connection to emit that signal when the button is pressed.
The Editor class code is straight out of the QtCreator wizard except for two edits I'm highlighting below. The designer form has two relevant controls: a QLineEdit (lineEdit) and a QPushButton (pushButton). I'll link to the full files below.
Editor.h
// in the Editor class declarations:
signals:
void textEdited(const QString &text) const;
Editor.cpp
// in the constructor, after ui->setupUi(this);
connect(ui->pushButton, &QPushButton::clicked, this, [this]() {
emit textEdited(ui->lineEdit->text());
});
Test harness, including MainWindow subclass and main()
#include <QApplication>
#include <QMainWindow>
#include <QTabWidget>
#include <QBoxLayout>
#include <QMessageBox>
#include "Editor.h"
class MainWindow : public QMainWindow {
Q_OBJECT
public:
MainWindow() : QMainWindow()
{
// set up a tab widget as the window central widget
QTabWidget *tabWidget = new QTabWidget(this);
setCentralWidget(tabWidget);
// the first/only page will contain all the editors in a vertical layout
QWidget *editorsPage = new QWidget(this);
editorsPage->setLayout(new QVBoxLayout());
// add the editors container page to tab widget
tabWidget->addTab(editorsPage, tr("Editors page"));
// Now create a number of editor widgets using our Editor class "template"
int layoutItems = 5; // number of editors needed, could be dynamic
for (int i=0; i < layoutItems; ++i) {
// Create an Editor instance with the tab page as parent
Editor *editor = new Editor(editorsPage);
// Add the editor widget to the tab page layout
editorsPage->layout()->addWidget(editor);
// A simple connection with the editor signal, as way of example.
connect(editor, &Editor::textEdited, this, [this](const QString &text) {
// just show a message box with the editor text
QMessageBox::information(this, tr("Text Edited"), text, QMessageBox::Ok);
});
}
}
};
int main(int argc, char **argv)
{
QApplication app(argc, argv);
MainWindow mainWindow;
mainWindow.show();
return app.exec();
}
#include "main.moc"
Links The full Editor code:
Editor.h
Editor.cpp
Editor.ui
The XML in Qt Creator is for UIC in QMake to generator code for you.
For example, QMake translates your mainwindow.ui to ui_mainwindow.h, and within you will find void setupUi(QMainWindow *MainWindow) with the actual code that creates and places the widgets.
Look at this code, the docs, and create and place the widgets yourself by code.
For example, adding 5 checkboxes to a groupbox by code:
QVBoxLayout *l = new QVBoxLayout(this);
ui->groupBox_4->setLayout(l);
for(int i=0; i<5; i++){
QCheckBox *c = new QCheckBox(this);
l->addWidget(c);
}

Accessing widgets inside QStackedWidget

I am developing a Qt app with QtDesigner.
Previously it was quite easy to access specific widgets to do something with them like connecting signals. After I added QStackedWidget I can no longer access specific widgets with something like ui->stack->page1->widget.
Is there a way to do it somehow? Or should I always call findChild method? Or maybe it is possible to at least assign some of the nested widgets in stack widget to properties of the main windwo class?
QStackedWidget provides a method to get child widgets by index, as well as the current widget.
A quick example is as follows:
MOCed Header
class MyWidget: QWidget
{
Q_OBJECT
public:
using QWidget::QWidget
QWidget *ptr;
};
Source File
QStackedWidget *stackedWidget = new QStackedWidget;
stackedWidget->addWidget(new MyWidget); // index 0
stackedWidget->addWidget(new QWidget); // index 1
stackedWidget->addWidget(new MyWidget); // index 2
QVBoxLayout *layout = new QVBoxLayout;
layout->addWidget(stackedWidget);
setLayout(layout);
// do something specific with the first widget's ptr element
auto* widget = stackedWidget->widget(0);
auto* mywidget = qobject_cast<MyWidget*>(widget);
if (mywidget) {
mywidget->ptr->setObjectName("FirstPage");
}
Now, Qt uses virtual interfaces by default, so if you have a custom subwidget you need to extract, you can use qobject_cast. qobject_cast is basically a fast dynamic_cast, and works even without RTTI. In template-driven code, dynamic_cast is a bit of a code-smell: it means you lost useful type information too early. With virtual interfaces, the exact opposite is true: you should use qobject_cast as needed.
Why you get the widget layer by layer, if your widgets are added in Qt designer, you can get it by ui->widget directly.

How to Change the IconSize for Actions in QMenu?

I am trying to resize the Icons of QActions in the QMenu with the following code but it doesn't work.
QMenu *menu;
menu =new QMenu();
menu->setStyleSheet("QMenu::icon{height:20px;width:20px});"
I would really appreciate it if someone could provide a solution.
Here is the solution that worked for me:
QMenu *menu;
menu =new QMenu();
QToolButton *button=new QToolButton(menu);
button->setFixedSize(50,50);
QWidgetAction *action=new QWidgetAction(this);
action->setDefaultWidget(button);
menu->addAction(action);
We can set style sheet to manage icon size like this:
QAction *action = new QAction("Exit", this);
action->setIcon(QIcon(":/images/resources/exit.png"));
QMenu *menu = new QMenu("File");
menu->addAction(action);
menu->setStyleSheet("QMenu {icon-size: 200px;} QMenu::item {background: transparent;}");
ui->menubar->addMenu(menu);
But it will display in an Improper size, so it's better to use QToolBar.
In your cpp file type this:
ui->ToolBarName->setIconSize(QSize(50,50));
In Designer Click on your QToolbar and set iconSize.
Just stumbled across this after all these years. I remember I had this problem once and now again. This time I actually managed to solve it somewhat. It IS kinda weird tho and should receive some love at least documentation-wise.
The key is: You need to style QMenu AND QMenu::item If you just set the icon size via:
QMenu {icon-size: 40px;}
it will remain ignored until you also set something like
QMenu::item {background: transparent;}
Unfortunately this resets the menu stylesheet and you need to do something about the hover state to make it usable. But well.
Seems this works for me.
(also posted this on the qt forums)

Show QDialog create in QtDesigner

I created next to my MainWindow a second form as QDialog with the Qt Designer.
My problem is how to show this Dialog by click a Button in the MainWindow.
If I use following code it creates a newDialog but I want to use the form I created in Qt Designer. How I can embed it?
QDialog *myDialog = new QDialog;
myDialog->show();
First you need to create in QtDesigner a new dialog. In that dialog add all the stuff that you need and then save all the changes. The file that you will create will be a .ui. Add this file to your project.
After this create a header file and cpp file with the name that you want( it would be better if you use the same name as de ui file ). Then in the .h:
namespace Ui {
class QDialogExample;
}
class QDialogExample : public QDialog
{
Q_OBJECT
public:
explicit QDialogExample(QWidget *parent = 0);
~QDialogExample();
private:
Ui::QDialogExample *ui; // This will be the acces to the widgets defined in .ui
};
And then in the .cpp file:
QDialogExample::QDialogExample(QWidget *parent) :
QDialog(parent),
ui(new Ui::QDialogExample)
{
ui->setupUi(this); // This will init all the widgets
}
Then in the slot of the QAction or your custom button add the call to
QDialogExample pDialogExample = new pDialogExample( this );
pDialogExample->show();
Anyway, to learn how it works, an all the process it would be advisable to use QtCreator, this is very helpfull in the creation of the dialogs and all the widgets that you need.

Qt - How to override a widget?

I've created a form using the Qt designer which includes a QToolButton.
I can turn it using ui->favouriteToolButton.
I used the designer because it's much more simple then code all the gui.
Problem is that specifically for this ToolButton i want to code by creating its own class:
class FavouriteMenu : public QToolButton
{
Q_OBJECT
public:
explicit FavouriteMenu(QWidget *parent = 0);
//~FavouriteMenu();
private:
QMenu *menu; //MENU
QAction *addToBookmarkAct;
QAction *editBookmarkAct;
...
I want to be able to override ui->favouriteToolButton with a new instance of FavouriteMenu.
How can i do it?
Right click on the button in designer and select "Promote To..."
Under Promoted class name enter "FavouriteMenu"
Under Header File enter the filename of the .h file
Click "Add"
Click "Promote"