How can I tell QWidget to show in the same place that it was before it was hidden? - qt

If I create and show a top-level QWidget, drag it to a new position on the desktop, then call widget->hide() followed by widget::show(), it generally reappears in a different place from where it was previously.
I can add code to the subclass which saves and restores its geometry, but I'm wondering if there's an in-Qt system for giving hints to the window manager as to where the widget should appear when shown.
Is there a nice way to do this?

I create a "Qt Widgets Application" and drag in a QPushButton, then add the following code:
void Widget::on_pushButton_clicked()
{
hide();
show();
}
Run the program, drag the widget to a new position, and then click on the button and I find that the widget is still in the same place, not like you said

Related

How to start second QDialog window on side of MainWindow?

I have a application in Qt, and a MainWindow. Now, I also added a helping QDialog which can be hooked up. This QDialog does not influence the programmflow, it just displays information.
But, it always starts on top of the MainWindow :/
SO I would like to start it on the side of the main window, so that it does not hinder the view to the main window?? How?
In my opinion , You should try this,
Use the overload of the QWidget::setParent() function to change the ownership of a QDialog widget, using set as NULL(but It will not share the parent's taskbar entry).
QDialog::show() returns control to the caller immediately, So it will not interfere in mainwindow flow.
Let's say, your application is in the full screen mode. Where then the QDialog should appear? It will be shown on the top and you won't be satisfied again.
If it doesn't influence the flow of the app then you are using it to communicate some sort of message. Can you use different ways? For instance, QStatusBar?
If not, why not to split the mainWindow with QSplitter or similar classes and provide an area where you can post all the info messages?
It sounds you want modaless dialog. In main window, use a slot to create the dialog.
void MainWindow::show_dialog()
{
if ( pDialog== NULL )
pDialog= new XYZ_Dialog(this);
QPoint p = pos();
QSize s = frameSize();
pDialog->setGeometry(p.x()+s.width(), p.y(), s.width()*1/2, s.height());
pDialog->show();
}
What I try to say is if the position of the new dialog bothers you, you set the position and size of it, before showing it.

Qt: display qgraphicsitem in a widget

I got a QGraphicsScene that contains QGraphicsItems. On clicking such an item I open a dialog. I now want the item to be displayed in an area of the dialog.
I tried a QGraphicsView (in the dialog) and "pointed" it to the item which works allmost perfectly. The problem is, that it is possible to click the item in the dialog which would open a new dialog.
So my question: is there a easy way to tell QGraphicsView to ignore any input events? If not, is there a easy way to display a QGraphicsItem within a widget?
I am feeling so stupid...
QGraphicsView::setInteractive(false) did the trick.
I am still able to move the icon with the mouse wheel but this can probably be avoided by restricting the scene rect with setSceneRect()
You can install an event filter, on the QGraphicsView, which ignores input events. The Qt documentation states:
In your reimplementation of this function, if you want to filter the event out, i.e. stop it being handled further, return true; otherwise return false.

How to remove bar from QMainWindow

I'm trying to delete this bar, butI can't get rid of it (it's locate just under the toolbar):
What is the name of that bar,how can I access it?
Thank you.
What you are calling the toolbar is actually the menu bar and what you are calling the other bar is actually an emtpy toolbar.
The most likely reason you have an empty toolbar is because you created your window using QtDesigner. If you choose a QMainWindow as your starting point, it automatically adds an empty menubar and an empty toolbar to the window. If you don't want the toolbar, find it in the Object Inspector on the right-hand side, right-click and select Remove Toolbar 'mainToolbar' (or whatever other name is the default).
If you added that tool bar you probably have a pointer to it? If yes, you can simply call:
removeToolBar(toolbar);
in your QMainWindow class. Otherwise you can remove all tool bars from the main window as:
QList<QToolBar *> allToolBars = mainWindow->findChildren<QToolBar *>();
foreach(QToolBar *tb, allToolBars) {
// This does not delete the tool bar.
mainWindow->removeToolBar(tb);
}
Below adds a little to #RobbieE's answer.
When creating a QMainWindow form, it creates mainToolBar for the user.
If you right click on it and select Remove Toolbar 'mainToolBar' it will be gone.
Or in code in the top of your constructor:
ui->setupUi(this);
delete ui->mainToolBar; // add this line
Hope that helps.

Show 2 Windows at Same time in Qt

How can i show two windows at same time ? Well i have a mainwindow ,then when i press a button i load a plugin which creates and return a qwidget, and set it as central widget setCentralWidget() ,but my app crashes .If i'm not wrong this anyway will show just one window at same time .
Code from plugin :
QWidget* PlPlugin::initPltter() {
plotter = new QWidget();
plotter->resize(200,300);
plotter->setWindowTitle("mypl");
plotter->show();
return plotter;
}
In my app i'm doing :
setCentralWidget(plotter->initPlotter());
Try not resizing or showing it. Its unnecessary if its going to become a central widget of the mainwindow.
Also, from your example there is nothing suggesting that you are trying to show two windows. What you are doing is creating another widget and setting it as a child of the main window. Do one or the other: Create the widget and show it directly, or create it and parent it under another.
Your MainWindow child shown in main.cpp, for second widget do this:
QWidget* PlPlugin::initPltter() {
plotter = new QWidget();
plotter->resize(200,300);
plotter->setWindowTitle("mypl");
plotter->show();
return plotter;
}
And don't do this
setCentralWidget(plotter->initPlotter());
It allow you to get MainWindow and widget in separate 'window'

QT How to remove the action menu item

when i add the widget to the main window, by default the action menu item will be present,
how to remove that?
menuBar()->setVisible(false);
verAction = new QAction(tr("&Version"),this);
menuBar()->addAction(verAction);
connect(verAction, SIGNAL(triggered()),this, SLOT(displayVersion()));
displayAction = new QAction(tr("&Display"),this);
menuBar()->addAction(displayAction);
connect(displayAction, SIGNAL(triggered()),this, SLOT(displayMessage()));
exitAction = new QAction(tr("&Exit"),this);
menuBar()->addAction(exitAction);
connect(exitAction, SIGNAL(triggered()),this, SLOT(close()));
Thanks
If you want to hide an QAction and display it when you need it, you can use the setVisible function.
If you want to remove the menu bar from the QMainWindow, you can use the QT_NO_MENUBAR preprocessor to remove all uses of a QMenuBar. If you are not using facilities provided by QMainWindow, maybe you can use a simple QWidget as main window in your application.
[Edit]
If you want to hide QActions at runtime, you will find them as member of the QMainWindow's UI. For example if you have a QAction named actionTest, you will access it like that: this->ui->actionTest->setVisible(false);
I know what you mean... you want to HIDE the DEFAULT CONTEXT MENU "Actions"....
You can do this in the Design section (not in code).
Then you see your Object-Stack on the right side like
MainWindow QMainWindow
centralWidget QWidget
webView QWebView
Now go to the property editor below...search for "contextMenuPolicy" and change it from "DefaultContextMenu" to "NoContextMenu" for every component if necessairy.
In order to remove the default context menu with the label "Actions" the following code may be used:
// Remove context menu from the all widgets.
QWidgetList widgets = QApplication::allWidgets();
QWidget* w=0;
foreach(w,widgets) {
w->setContextMenuPolicy(Qt::NoContextMenu);
}
Essentially, the same as the Joel's answer, but the code version :)
(Code taken from QFriendFeed sample from forum.nokia.com)

Resources