• 6258阅读
  • 11回复

菜鸟学习qt的第一个问题 [复制链接]

上一主题 下一主题
离线jyxhappy
 
只看楼主 倒序阅读 楼主  发表于: 2010-01-28
做C++ GUI Qt 3编程书中第一章的例子,就是AGE那个例子,其中,make时候QHBox *hbox = new QHBox(0);报错为call of overloaded `QHBox(int)' is ambiguous,改为QHBox *hbox = new QHBox();后不报错了,不明白为什么人家那么编程序就不报错,我的就报错呢?代码贴在下面
#include <qapplication.h>
#include <qhbox.h>
#include <qslider.h>
#include <qspinbox.h>

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);

    QHBox *hbox = new QHBox(0);
    hbox->setCaption("Enter Your Age");
    hbox->setMargin(6);
    hbox->setSpacing(6);

    QSpinBox *spinBox = new QSpinBox(hbox);
    QSlider *slider = new QSlider(Qt::Horizontal, hbox);
    spinBox->setRange(0, 130);
    slider->setRange(0, 130);

    QObject::connect(spinBox, SIGNAL(valueChanged(int)),
                     slider, SLOT(setValue(int)));
    QObject::connect(slider, SIGNAL(valueChanged(int)),
                     spinBox, SLOT(setValue(int)));
    spinBox->setValue(35);

    app.setMainWidget(hbox);
    hbox->show();

    return app.exec();
}
离线午小夜

只看该作者 1楼 发表于: 2010-01-28
構造函數的參數是 QHBox(QWidget* parent = 0);默認parent是0,即空.   //只是猜測...我不知道QHBox是什麽類...

如果是沒有父窗口,就不用帶參數,默認有.如果是當前窗體作parent,只要QHBox *hbox = new QHBox(this).
PS: QHBox 是什麽類?
[ 此帖被午小夜在2010-01-29 11:12重新编辑 ]
[操作系统版本]  Windows XP;Linux Ubuntu;Linux Fedora;
[Qt SDK版本]    4.7.0
[SDK 发布日期]  2010.05
[IDE(集成开发环境)] QtCreator
个人网页:http://hi.baidu.com/午小夜
學歷:Royal Jalidon
离线午小夜

只看该作者 2楼 发表于: 2010-01-28
age.cpp的代碼不是這個麽?
  1. #include <QApplication>
  2. #include <QHBoxLayout>
  3. #include <QSlider>
  4. #include <QSpinBox>
  5. int main(int argc, char *argv[])
  6. {
  7.     QApplication app(argc, argv);
  8.     QWidget *window = new QWidget;
  9.     window->setWindowTitle("Enter Your Age");
  10.     QSpinBox *spinBox = new QSpinBox;
  11.     QSlider *slider = new QSlider(Qt::Horizontal);
  12.     spinBox->setRange(0, 130);
  13.     slider->setRange(0, 130);
  14.     QObject::connect(spinBox, SIGNAL(valueChanged(int)),
  15.                      slider, SLOT(setValue(int)));
  16.     QObject::connect(slider, SIGNAL(valueChanged(int)),
  17.                      spinBox, SLOT(setValue(int)));
  18.     spinBox->setValue(35);
  19.     QHBoxLayout *layout = new QHBoxLayout;
  20.     layout->addWidget(spinBox);
  21.     layout->addWidget(slider);
  22.     window->setLayout(layout);
  23.     window->show();
  24.     return app.exec();
  25. }
[操作系统版本]  Windows XP;Linux Ubuntu;Linux Fedora;
[Qt SDK版本]    4.7.0
[SDK 发布日期]  2010.05
[IDE(集成开发环境)] QtCreator
个人网页:http://hi.baidu.com/午小夜
學歷:Royal Jalidon
离线yangfanxing
只看该作者 3楼 发表于: 2010-01-28
两个都没错~似乎无解释。
PHPWind好恶心。。。不想看这种界面。。。
离线午小夜

只看该作者 4楼 发表于: 2010-01-28
去這個帖子裏下載 比較新的 <C++ GUI QT4 Programming>電子書吧....               (QT3...崩潰ing)
http://www.qtcn.org/bbs/read.php?tid=19787
[操作系统版本]  Windows XP;Linux Ubuntu;Linux Fedora;
[Qt SDK版本]    4.7.0
[SDK 发布日期]  2010.05
[IDE(集成开发环境)] QtCreator
个人网页:http://hi.baidu.com/午小夜
學歷:Royal Jalidon
离线downstairs

只看该作者 5楼 发表于: 2010-01-28
貌似很玄啊。。用NULL试试
喜爱编程的猫头鹰
离线jyxhappy
只看该作者 6楼 发表于: 2010-01-29
QHBox 窗体为其子窗体提供了水平几何分布管理,这是Qt参考文档中说的,我理解为QSpinBox和QSlider是其子类。
离线jyxhappy
只看该作者 7楼 发表于: 2010-01-29
NULL不行哦,报一样的错误。
离线yangfanxing
只看该作者 8楼 发表于: 2010-01-29
引用第6楼jyxhappy于2010-01-29 10:38发表的  :
QHBox 窗体为其子窗体提供了水平几何分布管理,这是Qt参考文档中说的,我理解为QSpinBox和QSlider是其子类。

是其子【窗口之】类的东西。。。非子类。。。

我也试了下,(0) or () both OK。错不在代码,在某个环节;

用Qt4的话新建工程时选好Qt3Support选项
另外
#include <qapplication.h>
#include <qhbox.h>
#include <qslider.h>
#include <qspinbox.h>
这个类的写法:
#include <QApplication>
#include <QH3Box>
#include <QSlider>
#include <QSpinbox>
PHPWind好恶心。。。不想看这种界面。。。
离线jyxhappy
只看该作者 9楼 发表于: 2010-01-29
错不在代码,在某个环节_____能具体解释一下吗?到底是那个环节
离线yangfanxing
只看该作者 10楼 发表于: 2010-01-29
引用第9楼jyxhappy于2010-01-29 11:07发表的  :
错不在代码,在某个环节_____能具体解释一下吗?到底是那个环节

这个真不知道~~~
clean干净了重新qmake几遍估计就好了。。。
PHPWind好恶心。。。不想看这种界面。。。
离线jyxhappy
只看该作者 11楼 发表于: 2010-01-29
恩恩,谢谢啦,反正我的是qmake老多次了,都不行的,感觉斑竹说的那个原因还是比较有道理的,
快速回复
限100 字节
 
上一个 下一个