• 6494阅读
  • 10回复

菜鸟第二个问题同时感谢斑竹第一个问题的解答 [复制链接]

上一主题 下一主题
离线jyxhappy
 
只看楼主 倒序阅读 楼主  发表于: 2010-01-29
  做C++ GUI Qt 3编程书中第二章快速对话框设计,GoToCell例子,同样出现第一个的问题,就是书中代码在我的程序中报错,下面具体叙述,请问是不是我的qt安装问题,某个库或什么的没有安装进去导致的报错?

1、designer作界面,连接槽
2、uic生成cpp和h文件
/*-------------------h--------------------------------*/
#ifndef GOTOCELLDIALOG_H
#define GOTOCELLDIALOG_H

#include <qvariant.h>
#include <qdialog.h>

class QVBoxLayout;
class QHBoxLayout;
class QGridLayout;
class QLabel;
class QLineEdit;
class QPushButton;

class GoToCellDialog : public QDialog
{
    Q_OBJECT

public:
    GoToCellDialog( QWidget* parent = 0, const char* name = 0, bool modal = FALSE, WFlags fl = 0 );
    ~GoToCellDialog();

    QLabel* cellLocationLabel;
    QLineEdit* lineEdit;
    QPushButton* okButton;
    QPushButton* cancelButton;
    
    

public slots:
    virtual void enableOkButton();

protected:
    QVBoxLayout* GoToCellDialogLayout;
    QHBoxLayout* topLayout;
    QHBoxLayout* bottomLayout;

protected slots:
    virtual void languageChange();
private:
    void init();

};

#endif // GOTOCELLDIALOG_H


/*--------------------cpp-----------------------*/

#include "gotocelldialog.h"

#include <qvariant.h>
#include <qlabel.h>
#include <qlineedit.h>
#include <qpushbutton.h>
#include <qlayout.h>
#include <qtooltip.h>
#include <qwhatsthis.h>
#include "gotocelldialog.ui.h"


/*
*  Constructs a GoToCellDialog as a child of 'parent', with the
*  name 'name' and widget flags set to 'f'.
*
*  The dialog will by default be modeless, unless you set 'modal' to
*  TRUE to construct a modal dialog.
*/
GoToCellDialog::GoToCellDialog( QWidget* parent, const char* name, bool modal, WFlags fl )
    : QDialog( parent, name, modal, fl )

{
    if ( !name )
    setName( "GoToCellDialog" );
    GoToCellDialogLayout = new QVBoxLayout( this, 11, 6, "GoToCellDialogLayout");

    topLayout = new QHBoxLayout( 0, 0, 6, "topLayout");

    cellLocationLabel = new QLabel( this, "cellLocationLabel" );
    topLayout->addWidget( cellLocationLabel );

    lineEdit = new QLineEdit( this, "lineEdit" );
    topLayout->addWidget( lineEdit );
    GoToCellDialogLayout->addLayout( topLayout );

    bottomLayout = new QHBoxLayout( 0, 0, 6, "bottomLayout");
    QSpacerItem* spacer = new QSpacerItem( 10, 20, QSizePolicy::Expanding, QSizePolicy::Minimum );
    bottomLayout->addItem( spacer );

    okButton = new QPushButton( this, "okButton" );
    okButton->setEnabled( FALSE );
    okButton->setDefault( TRUE );
    bottomLayout->addWidget( okButton );

    cancelButton = new QPushButton( this, "cancelButton" );
    bottomLayout->addWidget( cancelButton );
    GoToCellDialogLayout->addLayout( bottomLayout );
    languageChange();
    resize( QSize(232, 86).expandedTo(minimumSizeHint()) );

    // signals and slots connections
    connect( lineEdit, SIGNAL( textChanged(const QString&) ), this, SLOT( enableOkButton() ) );
    connect( okButton, SIGNAL( clicked() ), this, SLOT( accept() ) );
    connect( cancelButton, SIGNAL( clicked() ), this, SLOT( reject() ) );

    // buddies
    cellLocationLabel->setBuddy( lineEdit );
    init();
}

/*
*  Destroys the object and frees any allocated resources
*/
GoToCellDialog::~GoToCellDialog()
{
    // no need to delete child widgets, Qt does it all for us
}

/*
*  Sets the strings of the subwidgets using the current
*  language.
*/
void GoToCellDialog::languageChange()
{
    setCaption( tr( "Go to Cell" ) );
    cellLocationLabel->setText( tr( "&Cell Location:" ) );
    okButton->setText( tr( "OK" ) );
    cancelButton->setText( tr( "Cancel" ) );
}



/*-------------------拷贝光盘中gotocelldialog.ui.h至工程代码如下-----------------*/
#include <qvalidator.h>


void GoToCellDialog::init()
{
    QRegExp regExp("[A-Za-z][1-9][0-9]{0,2}");
    lineEdit->setValidator(new QRegExpValidator(regExp, this));
}

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

/////////////////////////////////////////////////////////////////////////////////////////////////////////
3、progen生成pro,tmake生成Makefile
4、make后报错内容如下

gotocelldialog.ui.h: In member function `void GoToCellDialog::init()':
gotocelldialog.ui.h:6: parse error before `(' token
gotocelldialog.ui.h: In member function `virtual void
   GoToCellDialog::enableOkButton()':
gotocelldialog.ui.h:11: invalid use of undefined type `struct QLineEdit'
gotocelldialog.h:20: forward declaration of `struct QLineEdit'
make: *** [gotocelldialog.o] Error 1

也就是这两句
lineEdit->setValidator(new QRegExpValidator(regExp, this));
okButton->setEnabled(lineEdit->hasAcceptableInput());
的问题,注释掉就没问题了,出现了和我的第一个问题类似
QHBox *hbox = new QHBox(0);报错
QHBox *hbox = new QHBox();不报错

这种问题是不是因为qt安装的问题呢?同学的机器上同样测试这段代码也报错,不知大家运行程序是否报错?或我这么写有什么问题吗?代码附上,请求大家帮助。


离线jyxhappy
只看该作者 1楼 发表于: 2010-01-29
这是附件 报错代码 useuihtmakegotocell.rar (83 K) 下载次数:3
离线xinqingfly

只看该作者 2楼 发表于: 2010-01-29
gotocelldialog.ui.h中增加#include <qlineedit.h>和#include <qregexp.h>
菜鸟也是鸟
离线jyxhappy
只看该作者 3楼 发表于: 2010-01-29
gotocelldialog.ui.h中增加#include <qlineedit.h>和#include <qregexp.h>  不行
离线yangfanxing
只看该作者 4楼 发表于: 2010-01-29
原来是Qt3.2呃。。。
你就升下级吧。。。
PHPWind好恶心。。。不想看这种界面。。。
离线午小夜

只看该作者 5楼 发表于: 2010-01-29
引用第4楼yangfanxing于2010-01-29 11:22发表的  :
原来是Qt3.2呃。。。
你就升下级吧。。。


我都崩潰死了。。。
[操作系统版本]  Windows XP;Linux Ubuntu;Linux Fedora;
[Qt SDK版本]    4.7.0
[SDK 发布日期]  2010.05
[IDE(集成开发环境)] QtCreator
个人网页:http://hi.baidu.com/午小夜
學歷:Royal Jalidon
离线xinqingfly

只看该作者 6楼 发表于: 2010-01-29
把pro改成如下
TEMPLATE    = app
CONFIG        = qt qtopia warn_on release
HEADERS        = gotocelldialog.h
          
SOURCES        = gotocelldialog.cpp \
          main.cpp \
          gotocelldialog.ui.h
菜鸟也是鸟
离线xinqingfly

只看该作者 7楼 发表于: 2010-01-29
如果刚开始学还是从qt4开始吧,毕竟qt4是趋势,原来用的qt3,现在搞qt4,好多东西都是新的思路
菜鸟也是鸟
离线yangfanxing
只看该作者 8楼 发表于: 2010-01-29

引用第6楼xinqingfly于2010-01-29 11:30发表的  :
把pro改成如下
TEMPLATE    = app
CONFIG        = qt qtopia warn_on release
HEADERS        = gotocelldialog.h
          
SOURCES        = gotocelldialog.cpp \
          main.cpp \
          gotocelldialog.ui.h

gotocelldialog.ui.h是SOURCES???
PHPWind好恶心。。。不想看这种界面。。。
离线xinqingfly

只看该作者 9楼 发表于: 2010-01-29
gotocelldialog.ui.h是用户ui中自定义的函数的实现的地方,qt3里ui和自定义的函数很纠结,没像qt4这么规范
菜鸟也是鸟
离线jyxhappy
只看该作者 10楼 发表于: 2010-01-29
好,升级试试。
快速回复
限100 字节
 
上一个 下一个