I have this basic Qt program that comes from the "C++ GUI Programming with Qt" book. However, when I follow the instru开发者_运维技巧ctions (which differ since the book was written for Qt 4.4) my QLineEdit will not display text input from the keyboard.
The source code is as follows (I did not include the moc_*.cpp files). For those that don't like to re-assemble code via copy and paste the entire project can be downloaded from my subversion PlayGround at :
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
I recently had similar problem.
This is how I solved my issue:
mainwindow->centralWidget()->releaseKeyboard();
I solved the problem, I didn't read carefully enough, I was supposed to create a Widget not a dialog box. Still curious as to why the dialog box would not take input. I think this has to do with the fact a QDialogBox is not a top-level widget?
I also had the same situation.
When I use this->setWindowFlags(Qt::Popup| Qt::FramelessWindowHint);
than the input not working. If this line is removed, the input works fine, but then I can not hide the widget.
精彩评论