[SOLVED] help in getting focus to the dialog
-
as soon as my program loads, i have a dialog displayed over top of the mainwindow. The dialog is not focused but the mainwindow is. the tab key works for the mainwindow but instead i would like it to work with the dialog. how to set focus to the dialog so that it acts at if i clicked on the title of the dialog.
I have tried all of the following without any luck in getting focus to the dialog.
@ this->clearFocus();
this->setFocus();
this->raise();
this->activateWindow();@EDIT: this topic is solved. go to page 2 to find out how to set focus to the dialog when setModal is false.
-
Have you tried setModal on the dialog?
-
yes setModal to true works but i need to interact with the mainwindow when the dialog is displayed. therefore, i have setModal to false.
-
Where were you calling the method(s) listed above in your code from? What's triggering them?
-
I was calling the methods from the mainwindow.cpp file to load the dialog.cpp file. also, calling setFocus directly from within the dialog.cpp has no effect.
-
[quote author="kalster" date="1312954398"]yes setModal to true works but i need to interact with the mainwindow when the dialog is displayed. therefore, i have setModal to false. [/quote]
What kind of interaction do you want? Anything signals and slots could help with?
-
i am looking for tab interaction. the tab key must work when the dialog displays. currently when the dialog displays over top of the mainwindow, the tab key works for the mainwindow, I am not sure about the signal and slots. it does not look like they could work but i could be wrong
-
What are you wanting the tab key to do, exactly? Select the dialog? Or are you looking to be able to tab through the widgets on the main window, and then have the tab-action continue onto the dialog's widgets, too? Or something else?
-
when the dialog displays, there are widgets on the dialog that can be tabbed through but only those widgets on the dialog. The user would also have the option to click widgets on the mainwindow. this is why i have setmodal to false
-
So to make sure I understand, you want to be able to interact with the mainwindow, but you want the dialog to always keep keyboard focus?
-
yes. here is the code to the displaying of the dialog.
dialog.h
@#ifndef DIALOG_H
#define DIALOG_H#include <QDialog>
namespace Ui {
class Dialog;
}class Dialog : public QDialog
{
Q_OBJECTpublic:
explicit Dialog(QWidget *parent = 0);
~Dialog();private:
Ui::Dialog *ui;
};#endif // DIALOG_H@
mainwindow.h
@#ifndef MAINWINDOW_H
#define MAINWINDOW_H#include <QMainWindow>
class Dialog;
namespace Ui {
class MainWindow;
}class MainWindow : public QMainWindow
{
Q_OBJECTpublic:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();private:
Ui::MainWindow *ui;
Dialog *tt;
};#endif // MAINWINDOW_H@
dialog.cpp
@#include "dialog.h"
#include "ui_dialog.h"Dialog::Dialog(QWidget *child) :
QDialog(child),
ui(new Ui::Dialog)
{
ui->setupUi(this);
}Dialog::~Dialog()
{
delete ui;
}@mainwindow.cpp
@#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "dialog.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
Dialog *tt = new Dialog(this);
tt->show();
}MainWindow::~MainWindow()
{
delete ui;
}@ -
I'm not sure offhand (it's late here and I'm tired). You may be able to play with the "focusPolicy":http://doc.qt.nokia.com/latest/qwidget.html#focusPolicy-prop on your MainWindow (or it's components) and your Dialog. But that's just speculation. I'm not sure on what the details would be to implement such a thing.
-
ok. i will read the focusPolicy. thank you for your help Mlong. goodnight.
-
so if i click the pushbutton from the mainwindow, the dialog displays in focus but if i load the dialog when the mainwindow loads then the dialog does not have focus. is there a way to simulate a button being pressed? can i have some examples please?
-
You can't set the focus to the dialog in the constructor of the main window, as during this time, the main window has no focus.
The focus is set to the main window later on and after that time, you can move the focus, not before. -
We have a similar setup in our application. We do it this way:
- in main method main() instantiate the MainWindow subclass
- call mainWindow->show()
- call mainWindow->slotShowDialog()
- call app.exec()
-
this topic is solved. I showed the mainwindow first and then the dialog right after it as in this code and it works great. i stumbled on this fix after i read Volker post.
comment the w.show(); in main.cpp and add this code to your mainwindow.cpp file
@this->show();
Dialog->show();@