• 4574阅读
  • 4回复

为什么自定义类的对象无显示(已解决) [复制链接]

上一主题 下一主题
离线yunyun0220
 
只看楼主 倒序阅读 楼主  发表于: 2008-05-18
— 本帖被 XChinux 执行加亮操作(2008-05-18) —
程序代码如下:
--------counter.h--------------------------------------
#include<QLabel>
#include<QWidget>
#include"stdio.h"
#include<QString>
#include<Qt>
class counter:public QWidget
{
    Q_OBJECT
    public:
        counter();
    public slots:
        void IncCounter();
        void DecCounter();
    private:
        int Counter;
        QLabel *label;
};


----------------------counter.cpp---------------------------------

#include"counter.h"
counter::counter()
{
    Counter=0;
    label=new QLabel("0",this);
}
void counter::IncCounter()
{
    char str[30];
    sprintf(str,"%d",++Counter);
    label->setText(str);
}

void counter::DecCounter()
{
    char str[30];
    sprintf(str,"%d",--Counter);
    label->setText(str);
}


-----------------------------------------mainwindow.h-------------------------

#include<QPushButton>
#include"counter.h"

class MainWindow: public QWidget
{
    Q_OBJECT
    public :
        MainWindow();
    private:
        QPushButton *AddButton;
        QPushButton *DecButton;
        counter *ct;
};


-------------------------------------------------mainwindow.cpp---------------------------

#include"mainwindow.h"
#include<QFont>

MainWindow::MainWindow()
{
    QFont f("Helvetica",14,QFont::Bold);
    setFont(f);
   
    AddButton=new QPushButton("&Add",this);
    AddButton->setGeometry(50,15,90,40);
    DecButton=new QPushButton("&Dec",this);
    DecButton->setGeometry(50,70,90,40);

    ct=new counter();
    ct->setGeometry(50,125,90,40);

    connect(AddButton,SIGNAL(clicked()),ct,SLOT(counter::IncCounter()));
    connect(DecButton,SIGNAL(clicked()),ct,SLOT(counter::DecCounter()));   
}


------------------------------------------main.cpp-----------------------------
#include"mainwindow.h"
#include<QApplication>

int main(int argc,char **argv)
{
    QApplication app(argc,argv);
    MainWindow mwindow;
    mwindow.setGeometry(50,20,200,200);
    mwindow.show();
    return app.exec();
}




程序运行完毕后只有两个按钮会显示。。。为什么会这样呢。。。谢谢啦~~~
[ 此贴被yunyun0220在2008-05-19 09:50重新编辑 ]
离线XChinux

只看该作者 1楼 发表于: 2008-05-18
没有设置counter的parent
二笔 openSUSE Vim N9 BB10 XChinux@163.com 网易博客 腾讯微博
承接C++/Qt、Qt UI界面、PHP及预算报销系统开发业务
离线yunyun0220
只看该作者 2楼 发表于: 2008-05-18
那就是说在counter构造函数要改为:
  counter(QWidget *parent=0)???
离线XChinux

只看该作者 3楼 发表于: 2008-05-18
或者setParent()
反正要设置其parent属性为父窗口。
二笔 openSUSE Vim N9 BB10 XChinux@163.com 网易博客 腾讯微博
承接C++/Qt、Qt UI界面、PHP及预算报销系统开发业务
离线yunyun0220
只看该作者 4楼 发表于: 2008-05-19
Re:为什么自定义类的对象无显示
多谢版主,为已经解决这个问题。不过为什么会这样呢~~我还是不太懂原理哦~~
快速回复
限100 字节
 
上一个 下一个