Save and Restore QToolbar position
-
I am building a GUI with a QMainWindow and QToolbars. There are three toolbars in my application. Two of them are created only if some internal conditions are fullfilled.
How can i save and restore the position of a QToolbar? Is it possible?
If i use the standard facilities QMainWindow::saveState and QMainWindow::restoreState, the layout is restored incorrectly, due to a non constant number of toolbars.
-
Just add some member variables to hold the states of your toolbars, when application is terminated serialize their values to a file, when application is ran again read that file and set your toolbars accordingly.
-
Suppose i have:
[code]
class MyClass
{
QMainWindow* m_wnd;
QToolBar* m_toolBar1;
QToolBar* m_toolBar2;
QToolBar* m_toolBar3;xxx saveState(QToolBar* toolBar) const
{
?
}void restoreState(const xxx&, QToolBar* toolBar)
{
?
}};
[/code]How do i save and restore position of m_toolBar? I tried with m_toolBar->saveState / restoreState and that did not work
-
First of all, use the code wrapper function when pasting code in the forum. Second - that code does not contain toolbars, only pointers to toolbars, in the constructor of your class you will call methods to create the toolbars, you can set the custom variables that hold your toolbar states - whether a toolbar is visible, whether it is floating, where is it docked and so forth, connect the signals emitted when the toolbars are arranged by the user to slots that again store the state in your custom variables. You can put a method that saves those user variables to a file in the destructor, and in the constructor check if the file exists, and if so, read in the data from it and restore the toolbars the same way the user left them when he quit the application.
QToolBar has NO save and restore state methods! Unless someone else knows a standard way to do it with the Qt API, you will have to do it yourself in the manner I keep on describing to you.
-
connect the signals emitted when the toolbars are arranged by the user to slots that again store the state in your custom variables
Which signals do you mean?
restore the toolbars the same way
Which way do you mean?It sounds like i should reprogramm the whole QToolbar logic in my code...
What i need is the following:
[code]
ostream << m_toolBar1->position();
ostream << m_toolBar1->orientation();
ostream << m_toolBar1->size();
...m_toolBar1->setPosition(getPosition(istream));
m_toolBar1->setOrientation(getOrientation(istream));
m_toolBar1->resize(getSize(istream));
[/code] -
No, you don't have to touch the logic of QToolBar at all. You only need to serialize a few variables that hold information on whether your toolbars are visible, docked or floating, and respectively where are docked or their floating position. When a toolbar is modified it emits a range of signals such as:
@void visibilityChanged ( bool visible )
void orientationChanged ( Qt::Orientation orientation )@you also have QToolBar member methods such as:
@bool isAreaAllowed ( Qt::ToolBarArea area ) const
bool isFloatable () const
bool isFloating () const
bool isMovable () const@ -
[quote author="ddriver" date="1331817648"]... You only need to serialize a few variables that hold information on whether your toolbars are visible, docked or floating, and respectively where are docked or their floating position...
[/quote]I do not need info about visibility or orientation, i just want to know how i can save and restore the position of a QToolbar... Is it possible?
-
Have you actually read all the stuff I posted? I already told you how, both in short and in detail, and you still ask? It is basic programming so sorry if I am not rushing to write the code for you. QToolBar already has all the methods you need to copy its state into your custom variables and to restore the state from those variables.
-
Please do not extend your posts...
[quote author="ddriver" date="1331816587"]QToolBar has NO save and restore state methods! Unless someone else knows a standard way to do it with the Qt API, you will have to do it yourself in the manner I keep on describing to you.[/quote]
[quote author="ddriver" date="1331818311"]I already told you how, both in short and in detail, and you still ask?[/quote]
So, it is impossible, isn't it?Otherwise, could you show me how to get and restore the position?
[code]
ostream << m_toolBar1->position();
...
m_toolBar1->setPosition(getPosition(istream));
[/code] -
Can't you read English? I already told you HOW to do it, by what type of logic you assume it is impossible? Judging from your responses it seems like you need to work on your C++ and basic programming skills before you rush into building applications.
QToolBar inherits a QPoint returning member function called pos from QWidget that holds the position of the toolbar in its parent widget.
Start off by a bool isToolBar1Created, set it to false, when your user creates the toolbar, then set it to true.
Then have another one, bool isToolBar1Docked, when the user docks the toobar, set it to true, when the user detaches the toolbar to be floating, set it to false.
Then have a third variable that depending on whether the toolbar is floating or not, contains either its position or the location it is docked at.
When the user quits the application, write the three variables to a file. When the application is ran again, read in the file, if the first variable is false, then you don't create a toolbar, if it is true, then you create the toolbar, when you create it, check the second variable, if it is true then read the third for the dock location and dock the toolbar in that location. If it is false, set the toobar to floating and move it to the position stored in the third variable.
100% possible as you see.
-
Please stop editing your posts.
[quote author="ddriver" date="1331819488"]I already told you HOW to do it, by what type of logic you assume it is impossible?[/quote]
You said everything, but not a word about a position. I need a postion, a relative position, a floation position, not visibility, not if its dockable or not, not orientation. A position. Just a position. -
[quote author="ddriver" date="1331819488"]
QToolBar inherits a QPoint returning member function called pos from QWidget that holds the position of the toolbar in its parent widget.[/quote]Also, learn how to use the PLENTIFUL and DETAILED Qt documentation.
bq. pos : QPoint
This property holds the position of the widget within its parent widget.You also have:
@void move ( const QPoint & )@
which, in case you cannot figure out, moves the widget to the QPoint location.
http://qt-project.org/doc/qt-4.8/qtoolbar-members.html
http://qt-project.org/doc/qt-4.8/qwidget.html#pos-propAs you probably (don't) see, it is not only ENTIRELY possible, but QUITE EASY...
I also suggest you remove that foolish "[Impossible in Qt!]" qualifier from your thread and rethink your strategy - I really don't think you will go far into programming if you expect others to do the thinking and reading for you.
-
It is cool how you change your posts in the past from the future...
-
Certainly not as cool as being a lazy ungrateful jackass to people who waste their time to help you. Good luck, I am done with you!
-
[quote author="ddriver" date="1331829830"]Certainly not as cool as being a lazy ungrateful jackass to people who waste their time to help you. Good luck, I am done with you![/quote]
I hope you will not come back, dude. -
ddriver: There is no need to call people names here. Just move on to another thread if somebody annoys you.
-
[quote author="ddriver" date="1331819988"][quote author="ddriver" date="1331819488"]
QToolBar inherits a QPoint returning member function called pos from QWidget that holds the position of the toolbar in its parent widget.[/quote]
This property holds the position of the widget within its parent widget.
You also have:
@void move ( const QPoint & )@
which, in case you cannot figure out, moves the widget to the QPoint location.
[/quote]It just does not work with QToolBar, dude.
Try it youself:[code]
QPoint posa(m_toolBar1->pos());
m_toolBar1->move(pos.x()+10, pos.y());
[/code]The toolbar stays where it was.
-
Both pos() and move() work for me perfectly well, when the toolbar is docked it returns and moves to coordinates, relative to the application window, when floating, it is relative to the active display, so the problem is all in your "TV", dude!
@Tobias Hunger - it wasn't a name, but a definition, it is offensive only when it is degrading, if it is justified and objective - I don't think there is something wrong with calling things what they are :)
-
O, really?
[code]
#include <QtGui>int main(int argc, char argv[])
{
QApplication a(argc, argv);
QMainWindow wnd1;
QMainWindow wnd(&wnd1);
QToolBar* m_toolBar1 = new QToolBar(wnd);
QAction* action1 = new QAction("test 1", m_toolBar1);
action1->setText("1");
m_toolBar1->addAction(action1);QToolBar* m_toolBar2 = new QToolBar(wnd); QAction* action2 = new QAction("test 2", m_toolBar2); m_toolBar2->addAction(action2); action2->setText("2"); wnd->addToolBar(m_toolBar1); wnd->addToolBar(m_toolBar2); wnd->setCentralWidget(new QLabel("text")); wnd1.show(); m_toolBar2->resize(200, 100); m_toolBar2->move(200, 200); return a.exec();
}
[/code]