解决Qt编写对话框出现的问题

3 篇文章 0 订阅

1.   VS2008中,Qt Designer设计完对话框 xxx.ui ,用 uic.exe 生成头文件,用法: uic xxx.ui>ui_xxx.h

2.   将生成的 ui_xxx.h 添加进工程,在工程中添加类 Cxxx ,

xxx.h 中从 QDialog 和 ui_xxx.h 中继承过来:

class CGoToCellDialog : public QDialog, public Ui::GoToCellDialog
{
	Q_OBJECT
public:
	CGoToCellDialog(QWidget* parent = 0);
	~CGoToCellDialog(void);

private slots:
	void on_lineEdit_textChanged();
};

xxx.cpp 中在构造函数中这样写:


CGoToCellDialog::CGoToCellDialog(QWidget* parent) : QDialog(parent)
{
	setupUi(this);

	QRegExp regExp("[A-Za-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()));
}

CGoToCellDialog::~CGoToCellDialog(void)
{
}

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

3.   其中, setupUi 中会将 QtDesigner 中画的界面显示出来, setupUi 还会自动创建信号槽,规则为要符合 on_objectName_signalName() 的命名管理的任意槽于相应的

objectName 的 signalName的信号连接到一起,在这里有一个叫 lineEdit 的文本框,所有会自动完成下面的代码

connect( lineEdit, SIGNAL(textChanged()), this, SLOT(on_lineEdit_textChanged())) 。


4.   至此,编译程序,出现如下错误:

错误 1 error LNK2001: 无法解析的外部符号 "public: virtual struct QMetaObject const * __thiscall Widget::metaObject(void)const " (?metaObject@Widget@@UBEPBUQMetaObject@@XZ) 
错误 2 error LNK2001: 无法解析的外部符号 "public: virtual void * __thiscall Widget::qt_metacast(char const *)" (?qt_metacast@Widget@@UAEPAXPBD@Z) 
错误 3 error LNK2001: 无法解析的外部符号 "public: virtual int __thiscall Widget::qt_metacall(enum QMetaObject::Call,int,void * *)" (?qt_metacall@Widget@@UAEHW4Call@QMetaObject@@HPAPAX@Z)
错误 4 fatal error LNK1120: 3 个无法解析的外部命令


这是因为在源文件中没有添加上 moc_xxx.cpp 文件,解决办法:

命令行:moc.exe xxx.h -o moc_xxx.cpp ,输出 moc_xxx.cpp ;将之添加至源文件中,再次编译,生成成功!


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值