Qt opening another window when first one has closed - c++

I´ve beeing programming Java for some time right now...Now that I got into C++ and Qt I am a bit lost about GUI Thread (EDT Thread) and Worker Thread
I am trying to make the main window of my application open only when the configuration window is closed.
I dont want to put the code for creating the main window in the OK button of my configuration window.
I tryed to make them modal but the main window still opens.....
Afther configuration is complete I still have to see if there is an application update...So its something like
EDIT: This is my main:
ConfigurationWindow *cw = new ConfigurationWindow();
//if there is no text file - configuration
cw->show();
//**I need to stop here until user fills the configuration
MainWindow *mw = new MainWindow();
ApplicationUpdateThread *t = new ApplicationUpdateThread();
//connect app update thread with main window and starts it
mw->show();

Try something like this:
#include <QtGui>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QDialog *dialog = new QDialog;
QSlider *slider = new QSlider(dialog);
QHBoxLayout *layout = new QHBoxLayout(dialog);
layout->addWidget(slider);
dialog->setLayout(layout);
dialog->exec();
qDebug() << slider->value(); // prints the slider's value when dialog is closed
QMainWindow mw; // in your version this could be MainWindow mw(slider->value());
w.show();
return a.exec();
}
The idea is that your main window's constructor could accept parameters from the QDialog. In this contrived example I'm just using qDebug() to print the value of the slider in the QDialog when it's closed, not passing it as a parameter, but you get the point.
EDIT: You might also want to "delete" the dialog before creating the main window in order to save memory. In that case you would need to store the parameters for the main window constructor as separate variables before deleting the dialog.

You have to learn about signals and slots. The basic idea is that you would send a signal when you configuration is finished. You put your QMainWindow in a member variable and call mw->show() in a slot of your main programm that is connected with the configurationFinished signal.

If your ConfigurationWindow is a QDialog, you could connect the finished(int) signal to the MainWindow's show() slot (and omit the show() call from main).

Related

How can i stop QT Widget from closing?

I have a class (Window) that inherits from QWidget (Not QMainWindow).
This is the important part of my main function.
QApplication app (argc, argv);
QMainWindow *window = new QMainWindow;
QMenuBar *tool_bar = new QMenuBar (window);
Window *graph_area = new Window (arguments);
window->setMenuBar (tool_bar);
window->setCentralWidget (graph_area);
window->show ();
app.exec ();
I thought that i would just override "closeEvent", but for some reason it doesn't get called when pressing the closing button.
I want to stop the user from closing the application if some process is still working.
Any ideas about how i might achieve that?
You can either:
Subclass QMainWindow, instead of QWidget
Override QMainWindow::closeEvent, instead of QWidget::closeEvent
Allocate your QMainWindow subclass on the stack, instead of on the heap, i.e.:
MyMainWindowSubclass window;
or:
Change your code to:
QApplication app(argc, argv);
Window w;
window->show();
app.exec();
and Window::closeEvent will be called.
Make your Window class an event filter and install it on the main window object. You’ll reimplement filterEvent method in Window. Do not forget to install it on the main window! And make sure that the filter filters the event out - the boolean result indicates whether you let the result through or filter it out. Look up the exact value needed to indicate either one - it’s a bad api and I never remember whether true or false does what I intend. It’s a classical case where an enumeration return value would work better, like Qt::Accept/Qt::Reject or some such.
I actually found another solution to my problem, that I find more pleasant. I can create a custom class mainwindow that inherits from QMainWindow and use it instead of QMainWindow. It might be more appropriate, because I will probably need some other changes to QMainWindow as well.

Non-modal QWidget dialog that stays on top of the window

I want a dialog which stays on top of my main window and not other windows. I derived a class and added some flags. If I call the dialog now with show() the dialog appears and is staying on top as long as I don't press a button or whatever. Then the dialog goes to background again.
Dial::Dial(QWidget *parent) : QWidget(parent)
{
this->setWindowFlags(Qt::Tool | Qt::Dialog);
// ...
Consequently, I looked into the docu and found this:
Indicates that the widget is a tool window. A tool window is often a
small window with a smaller than usual title bar and decoration,
typically used for collections of tool buttons. If there is a parent,
the tool window will always be kept on top of it.
Happily, I added this line into my singleton creating the dialog.
d->mainWindow = new Foo();
d->dial->setParent(d->mainWindow);
Now the dialog is just embedded into my central widget (QOpenGlWidget) and is not a dialog anymore. Somehow, I seem to lack understanding what the docu is telling me? How can I get the dialog stay on top of my application and what does the docu mean?
I'm not able to reproduce your problem. The following code will generate a QWidget that will allways stay on top of the QMainWindow:
#include "QApplication"
#include "QMainWindow"
#include "QLineEdit"
int main(int argc, char * argv[])
{
QApplication a(argc, argv);
QMainWindow w;
w.show ();
QWidget *pLineEdit = new QWidget(&w);
pLineEdit->setWindowFlags(Qt::Tool | Qt::Dialog);
pLineEdit->show ();
a.exec ();
}
Tested with Qt 5.9.
Not sure if you've already solved this by now but you can try the WindowStaysOnTopHint flag when you construct the dialog:
Qt::WindowFlags flags = this->windowFlags();
flags |= Qt::WindowStaysOnTopHint;
this->setWindowFlags(flags);
Then use show() instead of exec() to make it non-modal:
dlg->show();
You need to set the modality (documentation) of the widget, like this:
QWidget *dialog = new QWidget(window, Qt::Dialog);
dialog->setWindowModality(Qt::ApplicationModal);
dialog->show();
However, I'd recommend to use the pre-configured QDialog class, which handles all that stuff for you:
QDialog *dialog = new QDialog(window);
dialog->exec();
Use QDialog instead of QWidget, and pass the parent widget in its constructor function.
QDialog* pDlg = new QDialog(this);
pDlg->show();

How to hide mouse pointer on startup?

I have an embedded touch screen running linux and my app is Qt/C++.
I start the app with the function main which looks like this:
#include <QApplication>
#include <QThread>
#include <stdlib.h>
int main(int argc, char *argv[])
{
// Create the app
QApplication app(argc, argv);
// Hide the pointer
app.setOverrideCursor( QCursor( Qt::BlankCursor ) );
// Create main window
MainWindow *window = new MainWindow;
// Set up the UI and execute the App
window->show();
app.exec();
// tidy up
delete(window);
return 0;
}
The problem that I have is that when I run the program the cursor is showing. But as soon as I touch the screen (the app is a full-screen app) the cursor then disappears.
I have read that it may be a simple focus issue - i.e. that my app is not in focus or some-such. How can I make sure that my app is the focus at startup?
Or
Is there anything I am doing wrong to hide the mouse pointer?
---EDIT---
If I remove the line app.setOverrideCursor( QCursor( Qt::BlankCursor ) ); then I have focus on my app. So this line somehow makes my app lose focus.
So as a quick hack I put a QTimer in the MainWindow and set it for 100ms (just so that it will only get handled once the event processing starts - i.e. after app.execute()). Then I connected the timer event to a slot function that I call "getFocusOnMeNow()" which in which I have:
this->activateWindow();
this->setFocus();
And this works.
So now I would like to tidy this up. What function/slot can I call AFTER app.execute() within the MainWindow to get focus? I don't see a MainWindow::start() slot that I can overload...?
try
app.setCursorVisible(false);
may this help u.

Qt4 login window

I am writing a login window in Qt.
When the users clicks on OK, it should close the login window, show a "Connecting to server..." Widget, and open the main window once the connecttoserver method has done its job.
However, the widget appears only when the main window is shown, and disappears immediately (it shouldn't even close!)
How do I solve this issue ?
void LoginWindow::blah()
{
close();
QWidget widget;
widget.show();
//calls to the "connecttoserver method"
Main *main = new Main(student->getInfo()[0], student->getInfo()[1], student->getInfo()[2], view);
main->show();
}
}
QWidget is declared as an automatic on the stack so it is destroyed when the method returns. You want to declare it on the heap instead:
QWidget *widget = new QWidget();
widget->show();
In addition to atomice's answer, make sure you have set your application's quitOnLastWindowClosed to false, or else your application will terminate between closing your login window and opening your main window.

Set QLineEdit focus in Qt

I am having a qt question. I want the QLineEdit widget to have the focus at application startup. Take the following code for example:
#include <QtGui/QApplication>
#include <QtGui/QHBoxLayout>
#include <QtGui/QPushButton>
#include <QtGui/QLineEdit>
#include <QtGui/QFont>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QWidget *window = new QWidget();
window->setWindowIcon(QIcon("qtest16.ico"));
window->setWindowTitle("QtTest");
QHBoxLayout *layout = new QHBoxLayout(window);
// Add some widgets.
QLineEdit *line = new QLineEdit();
QPushButton *hello = new QPushButton(window);
hello->setText("Select all");
hello->resize(150, 25);
hello->setFont(QFont("Droid Sans Mono", 12, QFont::Normal));
// Add the widgets to the layout.
layout->addWidget(line);
layout->addWidget(hello);
line->setFocus();
QObject::connect(hello, SIGNAL(clicked()), line, SLOT(selectAll()));
QObject::connect(line, SIGNAL(returnPressed()), line, SLOT(selectAll()));
window->show();
return app.exec();
}
Why does line->setFocus() sets the focus on the line widget #app startup only if it is placed after laying out the widgets and if used before it's not working?
Keyboard focus is related to widget tab order, and the default tab order is based on the order in which widgets are constructed. Therefore, creating more widgets changes the keyboard focus. That is why you must make the QWidget::setFocus call last.
I would consider using a sub-class of QWidget for your main window that overrides the showEvent virtual function and then sets keyboard focus to the lineEdit. This will have the effect of always giving the lineEdit focus when the window is shown.
Another trick that might work is by using the singleshot timer:
QTimer::singleShot(0, line, SLOT(setFocus()));
Effectively, this invokes the setFocus() slot of the QLineEdit instance right after the event system is "free" to do so, i.e. sometime after the widget is completely constructed.
Perhaps this is an update as the last answer was in 2012 and the OP last edited the question in 2014. They way I got this to work was to change the policy and then set the focus.
line->setFocusPolicy(Qt::StrongFocus);
line->setFocus();
In Qt setFocus() is a slot, you can try other overloaded method which takes a Qt::FocusReason parameter like the line shown below:
line->setFocus(Qt::OtherFocusReason);
You can read about focus reason options in the following link:
http://doc.trolltech.com/4.4/qt.html#FocusReason-enum