Add widget with editable properties through code - c++

I've created a custom widget plugin that is a container( it overloads the qframe). In this container i would like to add children through the code. I've succeeded in doing this. However the children aren't editable or clickable in the designer. I know for children to be editable through the designer, they should be added as code to the XML, but i don't know how to do this.
Would anybody know if it's at all possible to do this?
The code I have, with arrowedFrame as my custom widget plugin class:
arrowedFrame::arrowedFrame(QWidget *parent, Qt::WindowFlags f) : (parent, f)
{
QLabel* testLabel = new QLabel(this);
}
this adds a new Label, as member (sorry i can't yet post pictures, but imagine a box with a label in it). But as i said this label isn't at all editable through the designer.

The solution I found to this is by taking this guide. And adding some things in the initialize function:
void PlotContainerPlugin::initialize(QDesignerFormEditorInterface *formEditor)
{
if (initialized)
return;
QExtensionManager *manager = formEditor->extensionManager();
myFormEditor = formEditor;
Q_ASSERT(manager != 0);
manager->registerExtensions(factory, Q_TYPEID(QDesignerContainerExtension));
initialized = true;
}
The first part just gets a manager:
QExtensionManager *manager = formEditor->extensionManager();
And then we use this manager to,
manager->registerExtensions(factory, Q_TYPEID(QDesignerContainerExtension));
register the plugin(a plotter I my case) with the designer.
Hope this helps:)

Related

QObject::findChild returns 0 for QLabels added to statusbar

I created a application running in a QMainWindow using qtcreator, so the typical way.
I added two 'manually' (meaning: not with the Form editor) created qlabels to the statusbar:
in the header:
QLabel *label_timestamp;
QLabel *contentLabel_timestamp;
in the constructor:
MainWin::MainWin(const CmdLineOptions &opts, QWidget *parent)
: QMainWindow(parent),
ui(new Ui::MainWin),
m_connectionStatusLabel(new QLabel),
m_client(new QMqttClient),
m_mqttmanager(new MQTTManager(m_client)),
m_mqttServerName("localhost")
{
ui->setupUi(this);
label_timestamp = new QLabel(this);
contentLabel_timestamp = new QLabel(this);
label_timestamp->setText("system time");
contentLabel_timestamp->setText("dd.mm.yyyy, hh:mm:ss:zzz"); /* just testing output */
statusBar()->addPermanentWidget(label_timestamp);
statusBar()->addPermanentWidget(contentLabel_timestamp);
}
If I do a
Label *label = findChild<QLabel *>(QString("contentLabel_")+objName);
elsewhere in this class implementation with objName being 'timestamp', of course, findChild() returns 0. It's working fine with other QLabels created using QtCreator in the form editor, findChild() finds them all. Isn't the statusbar widget and its content also a child of ui? Does somebody eventually know a way out of there?
I want to use findChild to generically fill my labels following a naming scheme with content I receive over MQTT, this is the background. Would be great if the statusbar content would need a special handling but could also be handled in this dynamic approach.
Thanks a lot
findChild uses the objectName, in the case of Qt Creator this establishes it in the MOC, but in your case you must establish it:
label_timestamp = new QLabel(this);
contentLabel_timestamp->setObjectName("label_timestamp");
contentLabel_timestamp = new QLabel(this);
contentLabel_timestamp->setObjectName("contentLabel_timestamp");
And then you can recover it with:
QLabel *label_1 = findChild<QLabel *>("label_timestamp");
if(label_1){
// some code
}
QLabel *label_2 = findChild<QLabel *>("contentLabel_timestamp");
if(label_2){
// some code
}

Qt - Any guideline on how to implement navigation between UI Forms?

I'm learning Qt right now, trying to make a simple application. What I'm trying to implement is a simple 'Welcome' screen, with two buttons ('Register' and 'Login'). The problem is on the page redirection to the other two pages.
Also, I have the screens already setup using the QtCreator (in .ui Forms format).
The only solution I could come up with, until now, was based on an example from Qt itself, which uses a QStackedWidget, adding QWidgets as pages. The problem is that those pages in the example are mounted programmatically (and I want to use the Forms that I have).
If I try this:
MainWindow::MainWindow() :
ui_home(new Ui::HomeView),
ui_register(new Ui::RegisterView) {
ui_home->setupUi(this);
ui_register->setupUi(this);
pagesWidget = new QStackedWidget;
pagesWidget->addWidget(ui_home->centralWidget);
pagesWidget->addWidget(ui_register->centralWidget);
...
}
It 'kind of works', but the result is horrible. The 'centralWidget' from my Forms is added to the 'pagesWidget', but the 'setupUi' before that really renders the 'Home' and 'Register' pages all at once, messing everything up.
So, real question is:
Is there any guideline on how to implement navigation between UI Forms?
Secondly:
How can I retrieve the QWidget from my UI Form and add to a QStackedWidget, without rendering it?
I am using Ubuntu 12.04, with QtCreator 3.2.1.
Thanks in advance.
You do it wrong. You should create a "MainWidget" with 2 items: a) QStackedWidget, b) navigation panel (your buttons). Then you should set "MainWidget" as a central widget.
After it you can connect signals from navigation panel (clicked signals of "Registed" or "Login" buttons) to corresponding slots, that will select necessary widget on QStackedWidget
So, in your case, you need next 3 ui forms:
MainWindow (QStackWidget + 2 buttons)
LoginWidget
RegisterWidget
Pseudocode:
// RegisterWidget.cpp + you should have RegisterWidget.ui
RegisterWidget::RegisterWidget()
: public QWidget(NULL)
, ui( new Ui::RegisterWidget() )
{}
// LoginWidget.cpp + you should have LoginWidget.ui
LoginWidget::LoginWidget()
: public QWidget(NULL)
, ui( new Ui::LoginWidget() )
{}
// MainWindow.cpp + you should have MainWindow.ui
MainWindow::MainWindow()
: public QMainWindow()
, ui( new Ui::MainWindow() )
{
ui->setupUi(this);
// !!!!!!!!!!!!!!!!!!!
// Creating widgets here.
// Possible - setup communication between widgets with signals / slots
// !!!!!!!!!!!!!!!!!!!
m_loginForm = new LoginWidget();
m_regForm = new RegisterWidget();
ui->stackedWidget->addWidget( m_loginForm );
ui->stackedWidget->addWidget( m_regForm );
connect( ui->loginBtn, &QPushButton::clicked, this, &MainWindow::onLogin );
connect( ui->regBtn, &QPushButton::clicked, this, &MainWindow::onReg );
}
// private slots:
void MainWindow::onLogin()
{
ui->stackedWidget->setCurrentWidget( m_loginForm );
}
void MainWindow::onReg()
{
ui->stackedWidget->setCurrentWidget( m_regForm );
}
And don't forget about layouts. Because you may see nothing if your widgets will have 1x1px size.
How can I retrieve the QWidget from my UI Form and add to a QStackedWidget, without rendering it?
You may use QWidget::hide() method.

How to change contents of QMainWindow dynamically

I have a QMainWindow that starts out with nothing but a menubar with a menu that has two options. When the first is clicked the window should be populated with QLabels and various input widgets to recieve data. When the second option is clicked the window should be populated with a QTextEdit(obviously removing whatever was on the window at the time)
The following is code I have tried :
void OrderWindow::displayAddOrder(){
QVBoxLayout* tlayout = new QVBoxLayout();
QHBoxLayout* row = new QHBoxLayout();
row->addWidget(nameLbl);
tlayout->addLayout(row);
qDeleteAll(children());
delete layout();
setLayout(tlayout);
}
It's a bit messy since I've been trying various things. When I click on a menu option with this code it simply says the application has stopped working.
Any help would be appreciated.
You have at least the following options:
Always show the actual widget, and hide the rest. This is simple in case of two widgets like in your example. You could use this technique with the observer design pattern for any number of widgets.
Use the QStackedWidget class which basically behaves the way as your custom observer pattern implementation would be, although you will need to use an extra class for this.
Therefore, I would suggest to write the following code:
orderwindow.h
...
class QStackedWidget;
class OrderWindow
{
...
public:
explicit OrderedWindow(QWidget *parent);
...
private:
QStackedWidget m_stackedWidget;
...
}
...
orderwindow.cpp
#include "orderwindow.h"
#include <QStackedWidget>
...
OrderWindow::OrderWindow(QWidget *parent)
: QWidget(parent)
, m_stackedWidget(new QStackedWidget(this))
{
QWidget *firstPageWidget = new QWidget;
QWidget *secondPageWidget = new QWidget;
m_stackedWidget->addWidget(firstPageWidget);
m_stackedWidget->addWidget(secondPageWidget);
QVBoxLayout *layout = new QVBoxLayout;
layout->addWidget(stackedWidget);
setLayout(layout);
}
...
void OrderWindow::displayAddOrder() {
m_stackedWidget->setCurrentWidget(nameLbl);
}
...
you can use a QStackedWidget
start with showing an empty page and then show the correct page as needed:
there is then no need to mess with adding or removing widgets
Yes, you can use a QStakedWidget if your input options are fixed. If it's not, I suggest you to use an abstract factory pattern to create the stacked widget content. This woluld make your code more readable.

Qt: add a file selection field on the form (QLineEdit and "browse" button)

I need to display QLineEdit with "Browse" button at my form. When user clicks button, QFileDialog should be opened, and so on.
This is pretty common thing, but I can't find ready-made solution for that. I expected in Qt Designer some widget like QFileSelect, or something like that, but found nothing similar.
Should I implement it by hand? Or, what is the correct way to do this?
Should I implement it by hand? Or, what is the correct way to do this?
Yes, I agree with you that it is a common thing, but unfortunately you will need to implement this yourself. The good news is that you can do this easily by something like this:
MyMainWindow::createUI()
{
label = new QLabel("foo");
button = new QPushButton("Browse");
connect(button, SIGNAL(clicked()), SLOT(browse()));
layout = new QHorizontalLayout();
layout->addWidget(label);
layout->addWidget(button);
setLayout(layout);
}
void MyMainWindow::browse()
{
QString directory = QFileDialog::getExistingDirectory(this,
tr("Find Files"), QDir::currentPath());
if (!directory.isEmpty()) {
if (directoryComboBox->findText(directory) == -1)
directoryComboBox->addItem(directory);
directoryComboBox->setCurrentIndex(directoryComboBox->findText(directory));
}
}

Modify a tab in a QTabWidget where each tab represents a QTableView

I have a tab widget where every tab is a QTableView. I would like to be able to pass an updated model (QModelIndex) into each tab whenever the contents of that tab need to change.
The alternative (and nastier way) is for me to delete all the tabs, and then recreate them.
I know I can get the widget in the tab by doing something like:
tabWidget->widget(i);
This will return a widget, which is really a QTableView, but I want to update the model that is in that widget without having to delete and recreate the tab.
Thank you!
P.S. This is my current attempt...
for (int i = 0; i < tableView.size(); i++)
{
tabWidget->setCurrentWidget(tableView.at(i));
QTableView* updatedTable = (QTableView*)tabWidget->currentWidget();
updatedTable->setModel(dataModel);
tableView.replace(i, updatedTable);
}
It's not clear why you can't keep the QTableView widget and just change the model, as in your code. Doesn't the view refresh without this tableView.replace thing?
There doesn't appear to be a direct API for replacing the widget you put in with addTab() without going through a tab removal step. But instead of inserting the QTableView directly, you could instead call addTab() on a dummy widget that has a layout in it with a single item. A QStackedLayout, for instance:
QWidget* dummy = new QWidget;
QStackedLayout stackedLayout = new QStackedLayout;
stackedLayout->addWidget(tableView);
dummy->setLayout(stackedLayout);
tabWidget->addTab(dummy);
Then later, when you want to replace the tableView with a new one:
QWidget* dummy = tabWidget->currentWidget();
QStackedLayout newStackedLayout = new QStackedLayout;
newStackedLayout->addWidget(newTableView);
delete dummy->layout();
dummy->setLayout(newStackedLayout);
I still wonder what this is buying you that reusing the old table view couldn't do.