Qt QDialog问题:QLineEdit无法输入

3

我有一个基本的Qt程序,来自于“C++ GUI Programming with Qt”一书。然而,当我按照指示操作时(因为该书是针对Qt 4.4编写的),我的QLineEdit将无法显示从键盘输入的文本。

源代码如下(我没有包含moc_*.cpp文件)。如果您不喜欢通过复制和粘贴重新组装代码,则可以从我的Subversion PlayGround下载整个项目:

http://matthewh.me/PlayGround/branches/Qt4Devel/gotocell/ USER=guest PASSWORD=guest

#include <QApplication>
#include <QDialog>

#include <iostream>
#include "gotocelldialog.h"

int main( int argc, char *argv[ ] )
{
    QApplication app( argc, argv );
    GoToCellDialog *dialog = new GoToCellDialog;
    dialog->show( );
    dialog->activateWindow( );
    dialog->lineEdit->activateWindow( );
    return app.exec( );
}

#include <QtGui>
#include <iostream>

#include "gotocelldialog.h"

GoToCellDialog::GoToCellDialog( QWidget *parent )
    : QDialog( parent ) /* Call Parents Constructor */
{
    setupUi( this );
    lineEdit->setEchoMode( QLineEdit::Normal );
    QRegExp regExp( "[A-Z][a-z][1-9][0-9]{0,2}" );
    lineEdit->setValidator( new QRegExpValidator( regExp, this ) );
    //connect( okButton, SIGNAL( clicked( ) ), this, SLOT( accept( ) ) );
    //connect( cancelButton, SIGNAL( clicked( ) ), this, SLOT( reject( ) ) );
}

void GoToCellDialog::on_lineEdit_textChanged( )
{
    //okButton->setEnabled( lineEdit->hasAcceptableInput( ) );
}

#ifndef GOTOCELLDIALOG_H
#define GOTOCELLDIALOG_H

#include <QDialog>

#include "ui_gotocelldialog.h"

/* Adapter Class */
class GoToCellDialog : public QDialog, public Ui::GoToCellDialog
{
    Q_OBJECT
public:
    GoToCellDialog( QWidget *parent = 0 );
private slots:
    void on_lineEdit_textChanged( );
};

#endif

/********************************************************************************
** Form generated from reading UI file 'gotocelldialog.ui'
**
** Created: Sat Aug 20 23:05:28 2011
**      by: Qt User Interface Compiler version 4.7.3
**
** WARNING! All changes made in this file will be lost when recompiling UI file!
********************************************************************************/

#ifndef UI_GOTOCELLDIALOG_H
#define UI_GOTOCELLDIALOG_H

#include <QtCore/QVariant>
#include <QtGui/QAction>
#include <QtGui/QApplication>
#include <QtGui/QButtonGroup>
#include <QtGui/QDialog>
#include <QtGui/QHeaderView>
#include <QtGui/QLineEdit>

QT_BEGIN_NAMESPACE

class Ui_GoToCellDialog
{
public:
    QLineEdit *lineEdit;

    void setupUi(QDialog *GoToCellDialog)
    {
        if (GoToCellDialog->objectName().isEmpty())
            GoToCellDialog->setObjectName(QString::fromUtf8("GoToCellDialog"));
        GoToCellDialog->resize(286, 110);
        lineEdit = new QLineEdit(GoToCellDialog);
        lineEdit->setObjectName(QString::fromUtf8("lineEdit"));
        lineEdit->setGeometry(QRect(92, 23, 165, 24));

        retranslateUi(GoToCellDialog);

        QMetaObject::connectSlotsByName(GoToCellDialog);
    } // setupUi

    void retranslateUi(QDialog *GoToCellDialog)
    {
        GoToCellDialog->setWindowTitle(QApplication::translate("GoToCellDialog", "Go to Cell", 0, QApplication::UnicodeUTF8));
        lineEdit->setPlaceholderText(QString());
    } // retranslateUi

};

namespace Ui {
    class GoToCellDialog: public Ui_GoToCellDialog {};
} // namespace Ui

QT_END_NAMESPACE

#endif // UI_GOTOCELLDIALOG_H
3个回答

2
我最近也遇到了类似的问题。
以下是我解决这个问题的方法:
mainwindow->centralWidget()->releaseKeyboard();

1

我也遇到了同样的情况。

当我使用 this->setWindowFlags(Qt::Popup| Qt::FramelessWindowHint); 时,输入无法正常工作。如果移除这行代码,输入就能正常工作,但是我无法隐藏小部件。


1

我解决了这个问题,我没有仔细阅读,我应该创建一个小部件而不是对话框。我仍然很好奇为什么对话框无法接受输入。我认为这可能与 QDialogBox 不是顶级小部件有关?


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接