PDA

View Full Version : Dynamic Handling of Hundreds of UI elements



mrsurge
7th February 2016, 22:50
Ok so here is what I've got... doubleSpinBox - doubleSpinBox_312. I made them all read only, I only want them the to be edited from within 1 dialog. Ive made one of them clickable through an eventFilter:



MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui->doubleSpinBox->installEventFilter(this);
QObjectList o_list = ui->doubleSpinBox->children();
for(int i = 0; i < o_list.length(); i++)
{
QLineEdit *cast = qobject_cast<QLineEdit*>(o_list[i]);
if(cast)
cast->installEventFilter(this);
}

}


and



bool MainWindow::eventFilter(QObject *obj, QEvent *event)
{
if(event->type() == QEvent::MouseButtonPress)
{
Dialog mdialog;
mdialog.setModal(true);
mdialog.exec();
}
return false;
}


I am wondering if its possible to automate this part



ui->doubleSpinBox->installEventFilter(this);
//~~~~~~~~^
QObjectList o_list = ui->doubleSpinBox->children();
// ~~~~~~~~^


and to make sure I can return a value to a function that corresponds to what spinbox has been selected so that the dialog can return the new value to the appropriate field.

I really like the parameters and the look that the spinbox with no arrows gives so that's how I arrived at this juncture. Perhaps this more a C++ question than a Qt question, in any event IDK cause I'm a n00b.

Qt 5

preemptive thank you

Metavoid
8th February 2016, 07:53
Hi
you could find all QDoubleSpinBox in mainwindow and then loop them
and do the setting of filter on each.

QList<QDoubleSpinBox *> spins = MainWindow->findChildren<QDoubleSpinBox *>();

http://doc.qt.io/qt-5/qobject.html#findChildren

anda_skoa
8th February 2016, 09:18
Derive from QDoubleSpinBox, implement the customization, use your spinbox instead of the default one.

Then you don't have to do anything in MainWindow::MainWindow() other then calling setupUi();

Cheers,
_

mrsurge
8th February 2016, 15:48
Derive from QDoubleSpinBox, implement the customization, use your spinbox instead of the default one.

Then you don't have to do anything in MainWindow::MainWindow() other then calling setupUi();

Cheers,
_

maybe you can go into greater detail

<-----n00b

anda_skoa
8th February 2016, 15:56
1) derive a custom class from QDoubleSpinBox
2) implement the event filter in that class and install it on "this" and on the line edit (you have direct access to the line edit through a protected method declared in the base class)
3) Use designer's "promote widget" function to replace the QDoubleSpinBoxes with your class.

Cheers,
_