• 6564阅读
  • 9回复

被继承的类的信号如何唤醒继承类的槽 [复制链接]

上一主题 下一主题
离线goldbalance
 
只看楼主 正序阅读 楼主  发表于: 2009-09-26
class a
{
       singal:
           void q();
           void w();
}
class b:public a
{
      slot:
          void e();
          void r();
     b() : a()
     {
         connect(this,SINGAL(q()),this,SLOT(r()));
         connect(this,SINGAL(w()),this,SLOT(e()));
     }
}
程序运行起来后,控制台提示找不到a:e()和a:r();
请问有什么其他的方法?
(因为e和r操作的数据都在b中,所以不能将e和r移到class a中)
离线foxyz

只看该作者 9楼 发表于: 2009-09-28
如果你自己加了slot或者signal,就需要宏Q_OBJECT
离线dbzhang800

只看该作者 8楼 发表于: 2009-09-26
可以考虑把 前 10几行代码贴出来,应该是语法错误
离线goldbalance
只看该作者 7楼 发表于: 2009-09-26
下面是我加上Q_OBJECT的错误信息,当然已经手动moc编译了browse.h了,并将moc_browse.cpp加入工程中。

1>------ Build started: Project: qt3_1, Configuration: Debug Win32 ------
1>Compiling...
1>moc_browse.cpp
1>d:\program\毕设资料——任开(刻盘)\renkai\程序源码\暑假\qt3_1\qt3_1\qt3_1\browse.h(12) : error C2143: syntax error : missing ';' before '*'
1>d:\program\毕设资料——任开(刻盘)\renkai\程序源码\暑假\qt3_1\qt3_1\qt3_1\browse.h(12) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>d:\program\毕设资料——任开(刻盘)\renkai\程序源码\暑假\qt3_1\qt3_1\qt3_1\browse.h(12) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>d:\program\毕设资料——任开(刻盘)\renkai\程序源码\暑假\qt3_1\qt3_1\qt3_1\browse.h(21) : error C2065: 'label' : undeclared identifier
1>d:\program\毕设资料——任开(刻盘)\renkai\程序源码\暑假\qt3_1\qt3_1\qt3_1\browse.h(21) : error C2061: syntax error : identifier 'QLabel'
1>d:\program\毕设资料——任开(刻盘)\renkai\程序源码\暑假\qt3_1\qt3_1\qt3_1\browse.h(24) : error C2065: 'label' : undeclared identifier
1>d:\program\毕设资料——任开(刻盘)\renkai\程序源码\暑假\qt3_1\qt3_1\qt3_1\browse.h(24) : error C2227: left of '->setPalette' must point to class/struct/union/generic type
1>        type is ''unknown-type''
1>d:\program\毕设资料——任开(刻盘)\renkai\程序源码\暑假\qt3_1\qt3_1\qt3_1\browse.h(26) : error C2065: 'label' : undeclared identifier
1>d:\program\毕设资料——任开(刻盘)\renkai\程序源码\暑假\qt3_1\qt3_1\qt3_1\browse.h(26) : error C2227: left of '->setFont' must point to class/struct/union/generic type
1>        type is ''unknown-type''
1>d:\program\毕设资料——任开(刻盘)\renkai\程序源码\暑假\qt3_1\qt3_1\qt3_1\browse.h(122) : error C2065: 'label' : undeclared identifier
1>d:\program\毕设资料——任开(刻盘)\renkai\程序源码\暑假\qt3_1\qt3_1\qt3_1\browse.h(122) : error C2227: left of '->setText' must point to class/struct/union/generic type
1>        type is ''unknown-type''
1>d:\program\毕设资料——任开(刻盘)\renkai\程序源码\暑假\qt3_1\qt3_1\qt3_1\browse.h(123) : error C2065: 'label' : undeclared identifier
1>d:\program\毕设资料——任开(刻盘)\renkai\程序源码\暑假\qt3_1\qt3_1\qt3_1\browse.h(123) : error C2227: left of '->show' must point to class/struct/union/generic type
1>        type is ''unknown-type''
1>d:\program\毕设资料——任开(刻盘)\renkai\程序源码\暑假\qt3_1\qt3_1\qt3_1\browse.h(136) : error C2065: 'label' : undeclared identifier
1>d:\program\毕设资料——任开(刻盘)\renkai\程序源码\暑假\qt3_1\qt3_1\qt3_1\browse.h(136) : error C2227: left of '->setText' must point to class/struct/union/generic type
1>        type is ''unknown-type''
1>d:\program\毕设资料——任开(刻盘)\renkai\程序源码\暑假\qt3_1\qt3_1\qt3_1\browse.h(137) : error C2065: 'label' : undeclared identifier
1>d:\program\毕设资料——任开(刻盘)\renkai\程序源码\暑假\qt3_1\qt3_1\qt3_1\browse.h(137) : error C2227: left of '->show' must point to class/struct/union/generic type
1>        type is ''unknown-type''
1>Build log was saved at "[url=file://d:\program\]file://d:\program\[/url]毕设资料——任开(刻盘)\renkai\程序源码\暑假\qt3_1\qt3_1\qt3_1\Debug\BuildLog.htm"
1>qt3_1 - 17 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========


可见label识别不了,请问一下这如何去改?
离线dbzhang800

只看该作者 6楼 发表于: 2009-09-26
这个,当然需要
离线goldbalance
只看该作者 5楼 发表于: 2009-09-26
引用第3楼dbzhang800于2009-09-26 00:48发表的 :
必须有这个宏!
Q_OBJECT

pictureflow就是那个滑动图片的代码,那里pictureflow.h已经有宏了,它的继承类还要有么?
离线goldbalance
只看该作者 4楼 发表于: 2009-09-26
引用第3楼dbzhang800于2009-09-26 00:48发表的 :
必须有这个宏!
Q_OBJECT

用这个的话,class browser就会有错误
也就是Q_OBJECT,下面的QLabel会出错
离线dbzhang800

只看该作者 3楼 发表于: 2009-09-26
必须有这个宏!
Q_OBJECT
离线goldbalance
只看该作者 2楼 发表于: 2009-09-26
//brows.h
#include <qapplication.h>
#include <qdir.h>
#include <qevent.h>
#include <qfileinfo.h>
#include <qimage.h>
#include <QTime>
#include "pictureflow.h"
class Browser: public PictureFlow
{
public:
    QLabel* label;
    QString filename;
    QStringList files;
    int cnt;

    Browser(): PictureFlow()
    {
        setWindowTitle("PictureFlow");
        findFiles();
        label = new QLabel(this);
        QPalette pa;
        pa.setColor(QPalette::WindowText,Qt::white);
        label->setPalette(pa);
        QFont labelfont("Times", 20, QFont::Bold);
        label->setFont(labelfont);
        cnt = 2;
        filename = files[cnt];

        connect(this,SIGNAL(doshowpre()),this,SLOT(setLabelpre()));
        connect(this,SIGNAL(doshowpre()),this,SLOT(setLabelnext()));
    }

    void findFiles()
    {        
        //files << "*.XML"<<"*.xml";

        QDir dir("xml");

        //dir.setFilter(QDir::Files | QDir::Hidden | QDir::NoSymLinks);
        //dir.setNameFilters(files);

        QFileInfoList list = dir.entryInfoList();
        for (int i = 0; i < list.size(); ++i)
        {
            QFileInfo fileInfo = list.at(i);
            files.append(dir.relativeFilePath(fileInfo.fileName()));
        }
    }

    void keyPressEvent(QKeyEvent* event)
    {
        if(event->key() == Qt::Key_Escape || event->key() == Qt::Key_Enter ||
            event->key() == Qt::Key_Return)
        {
            event->accept();
            close();
        }

        // checking the speed of rendering
        if(event->key() == Qt::Key_F10)
            if(event->modifiers() == Qt::AltModifier)
            {
                qDebug("benchmarking.... please wait");
                const int blit_count = 10;

                QTime stopwatch;
                stopwatch.start();
                for(int i = 0; i < blit_count; i++)
                {
                    render(); repaint(); QApplication::flush(); QApplication::syncX();
                    render(); repaint(); QApplication::flush(); QApplication::syncX();
                    render(); repaint(); QApplication::flush(); QApplication::syncX();
                    render(); repaint(); QApplication::flush(); QApplication::syncX();
                    render(); repaint(); QApplication::flush(); QApplication::syncX();
                    render(); repaint(); QApplication::flush(); QApplication::syncX();
                    render(); repaint(); QApplication::flush(); QApplication::syncX();
                    render(); repaint(); QApplication::flush(); QApplication::syncX();
                    render(); repaint(); QApplication::flush(); QApplication::syncX();
                    render(); repaint(); QApplication::flush(); QApplication::syncX();
                }
                QString msg;
                int elapsed = stopwatch.elapsed();
                if( elapsed > 0 )
                    msg = QString("FPS: %1").arg( blit_count*10*1000.0/elapsed );
                else
                    msg = QString("Too fast. Increase blit_count");
                setWindowTitle( msg );
                event->accept();
                return;
            }

            // for debugging only: Alt+F11 cycles the reflection effect
            if(event->key() == Qt::Key_F11)
                if(event->modifiers() == Qt::AltModifier)
                {
                    qDebug("changing reflection effect...");
                    switch(reflectionEffect())
                    {
                        //case NoReflection:      setReflectionEffect(PlainReflection); break;
                    case PlainReflection:   setReflectionEffect(BlurredReflection); break;
                    case BlurredReflection: setReflectionEffect(PlainReflection); break;
                    default:                setReflectionEffect(PlainReflection); break;
                    }
                    event->accept();
                    return;
                }

                PictureFlow::keyPressEvent(event);  
    }
public slots:
    void setLabelpre()
    {
        if (cnt == 2)
        {
            cnt = files.size()-2;
        }
        else
        {
            cnt--;
        }
        filename = files[cnt];
        label->setText(filename);
        label->show();
    }
    void setLabelnext()
    {
        if (cnt == files.size())
        {
            cnt = 2;
        }
        else
        {
            cnt++;
        }
        filename = files[cnt];
        label->setText(filename);
        label->show();
    }
};
离线dbzhang800

只看该作者 1楼 发表于: 2009-09-26
把你的能反应问题的稍微完整的代码贴一下。

不然没办法判断一些关键代码是你代码中确实没写还是你贴出来是省略了
快速回复
限100 字节
 
上一个 下一个