QT3编译或运行时出现的错误及解决

本文详细解析了在使用Qt进行C++编程时遇到的常见错误和问题,包括编译错误、运行时错误、初始化错误等,并提供了相应的解决方法。通过本文,读者能够快速定位并解决开发过程中可能遇到的技术难题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.      编译时出错:构造函数××ד是私有的”

/usr/lib/qt-3.3/include/qwidget.h: In copy constructor ‘CDishTypePage::CDishTypePage(const CDishTypePage&)’:

/usr/lib/qt-3.3/include/qwidget.h:738: 错误:‘QWidget::QWidget(const QWidget&)’ 是私有的

cdishtypepage.h:10: 错误:在此上下文中

cmainformboard.cpp: In constructor ‘CMainFormBoard::CMainFormBoard(QWidget*, const char*)’:

cmainformboard.cpp:14: 附注:在这里第一次需要生成的方法 ‘CDishTypePage::CDishTypePage(const CDishTypePage&)’

cmainformboard.cpp:14: 错误:  initializing temporary from result of‘CDishTypePage::CDishTypePage(QWidget*, const char*)’

解决:

出现上面的错误是因为定义类对象的时候没有用指针:

CDishTypePage dishTypePage = new CDishTypePage(this, "dishTypePage");

应改成:

CDishTypePage *dishTypePage = new CDishTypePage(this, "dishTypePage");

 

2.      运行时出现下面的错误:

浮点数例外   (多半是因为出现除0现象)

段错误      (多半是因为使用了空指针,如对象还未初始化就调用了它的成员函数)

 

3.      编译时出错:变量×××有初始值设定,但是类型不完全

ccommfunctions.cpp:17: 错误:变量 ‘QImage qimage’ 有初始值设定,但是类型不完全

ccommfunctions.cpp:17: 错误:invalid use of undefined type ‘struct QImage’

/usr/lib/qt-3.3/include/qwindowdefs.h:74: 错误:forward declaration of ‘struct QImage’

ccommfunctions.cpp:18: 错误:嵌套名指定中使用了不完全的类型 ‘QImage’

ccommfunctions.cpp: At global scope:

ccommfunctions.cpp:14: 警告:‘QPixmap getFretchPixmap(int, int, const char*)’ 定义后未使用

make: *** [ccommfunctions.o] 错误 1

解决:

把#include <qimage.h>加在.h文件里就好了

 

4.      在头文件里定义常量的错误:ISO C++ 不允许成员 ‘M_ICON_WIDTH’ 的初始化

一定要static和const一起使用,否则提示错误:

const int M_ICON_WIDTH = 100;

ddishpage.h:30: 错误:ISO C++ 不允许成员 ‘M_ICON_WIDTH’ 的初始化

ddishpage.h:30: 错误:使 ‘M_ICON_WIDTH’ 成为静态的

应该写成:

static const int M_ICON_WIDTH = 100;

 

5.      指针对象使用成员函数时的错误:

对成员‘setText’的请求出现在‘((DPasswordForm*)this)->DPasswordForm::m_editPassword’中,而后者具有非类类型‘QLineEdit*’

对成员‘text’的请求出现在‘((DPasswordForm*)this)->DPasswordForm::m_editPassword’中,而后者具有非类类型‘QLineEdit*’

解决:

其实原因很简单:定义了QLineEdit的指针对象,而使用“.”来调用成员函数:

QLineEdit *le = new QLineEdit(this);

le.setText("");

改成:le->setText("");就好了

 

6.      找不到类的成员函数

/home/wj/cy2_dzcd/bpixmapbuttonbase.cpp:20: undefined reference to `CCommFunctions::getMonoPixmap(QPixmap)'

collect2: ld 返回 1

make: *** [cy2_dzcd] 错误 1

解决:

cpp文件里,看看是不是在定义的时候函数名前面忘记写类名了,

QPixmap getMonoPixmap(QPixmap pix)

改成:

QPixmap CCommFunctions::getMonoPixmap(QPixmap pix)

 

7.      设置背景色的错误

语句:

m_qivDishIconView->setPaletteBackgroundColor(new QColor(77, 00, 113));

错误:

ddishpage.cpp:38: 错误:从类型 ‘QColor*’ 到类型 ‘QRgb’ 的转换无效

ddishpage.cpp:38: 错误:  初始化实参 1,属于 ‘QColor::QColor(QRgb, uint)’

解决:

改成:m_qivDishIconView->setPaletteBackgroundColor(QColor(77, 00, 113));

 

8.      未引用头文件的错误

dmenupage.cpp:95: 错误:invalid use of undefined type ‘struct QObjectList’

/usr/lib/qt-3.3/include/qobjectdefs.h:161: 错误:forward declaration of ‘struct QObjectList’

解决:

在文件开头引入头文件:#include <qobjectlist.h>

 

9.      每次修改、保存源文件之后,再make工程都会出现找不到类定义或是其它奇快问题

可能是有些外部新生成的类文件没有引到QT Designer里:如从cvs上新下载下来,或是编译器自动生成的。

这时,关闭工程,重新打开,就会解决问题。

所以,每次启动QT Designer之前,现在工程目录下cvs update + make一下是一个好的习惯。

 

10.  widget显示的问题

一个Dialog窗口,只要定义一个widget对象就会在左上角出现一个类似label的区域,与底层同样式的时候不易看出来,给底层加个背景图就看出来了。

解答:实际上那是一个QWidget对象,在程序中会用到它的一些函数。若不希望它显示出来,可以用widget->setHidden(true);使它隐藏。

 

11.  关于背景图和paintEvent()的问题

代码如下:

#include <qapplication.h>

#include <qlabel.h>

#include <qlayout.h>

#include <qpixmap.h>

 

class MyButton : public QWidget

{

public:

    MyButton( QWidget *parent=0, const char *name=0 );

   

protected:

    void paintEvent( QPaintEvent *);

   

private:

    QLabel *label;

};

 

MyButton::MyButton( QWidget *parent, const char *name )

        : QWidget( parent, name )

{

    label = new QLabel( "label", this, "label" );

    QVBoxLayout *box = new QVBoxLayout(this);

    box->addWidget(label);

}

 

void MyButton::paintEvent( QPaintEvent *e)

{

    qDebug("BPixmapButtonBase::paintEvent");

}

 

class MyWidget : public QWidget

{

public:

    MyWidget( QWidget *parent=0, const char *name=0 );

};

 

MyWidget::MyWidget( QWidget *parent, const char *name )

        : QWidget( parent, name )

{

    MyButton *btn = new MyButton(this, "label" );

}

 

int main( int argc, char **argv )

{

    QApplication a( argc, argv );

 

    MyWidget w;

    w.resize(200, 120);

    QPixmap pix("images/ground1.png");

    w.setPaletteBackgroundPixmap(pix);

    a.setMainWidget( &w );

    w.show();

    return a.exec();

}

若去掉w.setPaletteBackgroundPixmap(pix);这句,或是换成w.setBackgroundPixmap(pix);则MyButton::paintEvent()不会被调用。到底有什么联系呢?想了很久也想不明白,很郁闷。

发现:在MyWidget里也定义piantEvent事件之后,在里面调用btn->repaint()或btn->update()就都是好使的。虽然还没太明白和setPaletteBackgroundPixmap()的关系,但应该可以用了.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值