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

最佳答案

我希望我正确理解了这个问题。这是您想要的最小示例。

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


{

ui-> setupUi(this);

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(this);

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);


}

10-07 15:20