Sharing same QToolbar amongst multiple QMainWindows
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?
Code:
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" )
{
this->addToolBar(Qt::TopToolBarArea, NewTB );
NewTB->setMovable(false);
NewTB->setStyleSheet(mainTB->styleSheet());
NewTB->addActions(mainTB->actions());
}
break;
}
}
}
Re: Sharing same QToolbar amongst multiple QMainWindows
There is no reason to share toolbars between main windows. It is the actions that should be shared, the toolbars should remain separate.
Re: Sharing same QToolbar amongst multiple QMainWindows
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()?
Re: Sharing same QToolbar amongst multiple QMainWindows
Quote:
Originally Posted by
Henry Blue Heeler
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.
Re: Sharing same QToolbar amongst multiple QMainWindows
Well, how should I create the toolbars? Please criticize the example code above.
Re: Sharing same QToolbar amongst multiple QMainWindows
Do not copy contents of one toolbar to the other. Have a function that will create identical toolbars instead.
Code:
tb->addAction(action1);
tb->addAction(action2);
// ...
return tb;
}
Re: Sharing same QToolbar amongst multiple QMainWindows
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.
Re: Sharing same QToolbar amongst multiple QMainWindows
Quote:
Originally Posted by
Henry Blue Heeler
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?
Re: Sharing same QToolbar amongst multiple QMainWindows
I want the same slots for all the toolbars and now it's working great.
Thanks again for the help!
Something like
Code:
{
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"));
// ...
}