PDA

View Full Version : Sharing same QToolbar amongst multiple QMainWindows



Henry Blue Heeler
28th December 2013, 05:20
I'm trying to use the same toolbar with a parent QMainWindow and its children QMainWindows.
Same actions, same stylesheet, same icons, etc.
The following likely violates all sense of Qt decency, but it kind of works, but only for the first few instantiations of the child QMainWindows.
I know I'm just using pointers, but is there a better way other than painstakingly building a new toolbar for each child QMainWindow?




SubWin::SubWin(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::SubWin)
{
ui->setupUi(this);
setAttribute(Qt::WA_DeleteOnClose);

foreach ( QWidget* widget, qApp->allWidgets() )
{
if ( QToolBar* mainTB = qobject_cast<QToolBar*>( widget ) )
{
if( mainTB->objectName() == "mainToolBar" )
{
QToolBar *NewTB = new QToolBar("New Tool Bar", 0);

this->addToolBar(Qt::TopToolBarArea, NewTB );
NewTB->setMovable(false);

NewTB->setStyleSheet(mainTB->styleSheet());

NewTB->addActions(mainTB->actions());
}
break;
}
}

}

wysota
28th December 2013, 13:03
There is no reason to share toolbars between main windows. It is the actions that should be shared, the toolbars should remain separate.

Henry Blue Heeler
28th December 2013, 16:56
I'm not really sharing toolbars, I'm adding a new toolbar for every new window. Sharing actions? Do I create a list of actions then addActions()?

wysota
28th December 2013, 20:18
I'm not really sharing toolbars, I'm adding a new toolbar for every new window.

Yes, and that's a proper approach. Of course not considering HOW you are creating those toolbars.

Henry Blue Heeler
28th December 2013, 21:07
Well, how should I create the toolbars? Please criticize the example code above.

wysota
28th December 2013, 21:13
Do not copy contents of one toolbar to the other. Have a function that will create identical toolbars instead.


QToolBar *createToolBar(QMainWindow *window) {
QToolBar *tb = new QToolBar(window);
tb->addAction(action1);
tb->addAction(action2);
// ...
return tb;
}

Henry Blue Heeler
29th December 2013, 02:44
A function for creating common toolbars is exactly what I've been working on. You confirmed my suspicions.
The code example at the top was trying to copy/clone the toolbar et al from the parent QMainWindow to the children.
I know, bad form.
I can't second guess why no QToolBar copy constructor is available, that would certainly make this whole exercise moot.

Thanks for the help.

wysota
29th December 2013, 13:52
I can't second guess why no QToolBar copy constructor is available, that would certainly make this whole exercise moot.

For exactly the same reason why any QObject derived class cannot be copied -- QObject instances are identities, not values. For instance, if you make a number of signal/slot connections to the toolbar, should a copy have those connections as well or not?

Henry Blue Heeler
31st December 2013, 23:57
I want the same slots for all the toolbars and now it's working great.
Thanks again for the help!
Something like


void MainWindow::CreateToolBar( QMainWindow *parent )
{
QToolBar *tb = parent->addToolBar("test");
QAction *actionPrint = tb->addAction( QObject::tr("Print Record"), this, SLOT(on_actionPrint_triggered()));
actionPrint->setIcon(QPixmap(":/Images/printer.bmp"));
actionPrint->setText(QObject::tr("Print Record"));
actionPrint->setToolTip(QObject::tr("Print Record"));
// ...
}