具有空父级的QDialog小部件始终可见

时间:2012-01-18 04:37:34

标签: c++ qt

我正在使用qt 3.3。我需要使用null父级的QDialog小部件始终可见而不是停留在顶部(WStyle_StaysOnTop),因为此标志会阻止对主应用程序的访问。我的QDialog小部件需要屏幕键盘功能。

1 个答案:

答案 0 :(得分:1)

我希望我正确理解这个问题。这是你想要的最小例子。

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)

{

UI-> setupUi(本);

QDialog *dia = new QDialog(this);
//Set the windowflags
dia->setWindowFlags(dia->windowFlags() | Qt::Tool);

dia->show();

QWidget *central = new QWidget;
QHBoxLayout *mainLayout = new QHBoxLayout;
QLineEdit *edit = new QLineEdit;

//Add sample QLineEdit to test the input focus for mainwindow
mainLayout->addWidget(edit);
central->setLayout(mainLayout);
setCentralWidget(central);

}

修改

如果您希望能够从systray最小化和最大化有问题的对话框,您必须为它创建QSystrayIcon和上下文菜单:

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)

{     UI-> setupUi(本);

QDialog *dia = new QDialog(this);

dia->setWindowFlags(dia->windowFlags() | Qt::Tool);

dia->show();

QWidget *central = new QWidget;
QHBoxLayout *mainLayout = new QHBoxLayout;
QLineEdit *edit = new QLineEdit;
mainLayout->addWidget(edit);
central->setLayout(mainLayout);
setCentralWidget(central);

//Create the icon for systray
//NOTE this icon is application wide 
QSystemTrayIcon *icon = new QSystemTrayIcon(QIcon(QPixmap("/usr/share/icons/oxygen/22x22/status/user-away.png")), dia);

icon->setVisible(true);

//Create context menu to manipulate the dialog
QMenu *contextMenu = new QMenu;

QAction *minimizeDialog = contextMenu->addAction("Minimize dialog");
QAction *restoreDialog = contextMenu->addAction("Restore dialog");

connect(minimizeDialog, SIGNAL(triggered()), dia, SLOT(hide()));
connect(restoreDialog, SIGNAL(triggered()), dia, SLOT(show()));

//Add it to the icon
icon->setContextMenu(contextMenu);

}