2013-08-27 52 views
2

如何让qDebug打印我的类是否存在或有关该类的信息?不能相信在互联网上没有关于这一点。我需要确保我的ink = new InkSpot(this;)实际上正在返回一些有效的东西。Qt/C++ - qDebug <<类

ink = new InkSpot(this); 
qDebug << ink; 

X:\Development\InkPuppet\inkpuppet.cpp:70: error: C3867: 'QMessageLogger::debug': function call missing argument list; use '&QMessageLogger::debug' to create a pointer to member

我一直在使用QMessageLogger尝试,但只得到各种错误。

我的程序崩溃,因为我做一些事情来ink.widget,所以我会后的代码为墨

调用它的代码是这样的:

头: InkSpot *ink;

void InkPuppet::testButton() 
{ 
    ink = new InkSpot(this); 
    qDebug() << ink->widget; 
    ui->testButton->setText("working"); 
} 

inkspot.h

#ifndef INKSPOT_H 
#define INKSPOT_H 

#include <QObject> 
#include <QWidget> 
#include <QPainter> 
#include <QPaintEvent> 
#include <QLabel> 

namespace Ui { 
class InkSpot; 
} 

class InkSpot : public QWidget 
{ 
    Q_OBJECT 
public: 
    explicit InkSpot(QWidget *parent = 0); 
    void draw(QPainter *painter); 
    QWidget *widget; 
    QLabel *label; 

signals: 

public slots: 

protected: 
    void paintEvent(QPaintEvent *event); 

private: 
    Ui::InkSpot *ui; 

}; 

#endif // INKSPOT_H 

inkspot.cpp

#include "inkspot.h" 
#include "inkpuppet.h" 
#include "ui_inkpuppet.h" 

#include <QtCore> 
#include <QtGui> 
#include <QWidget> 
#include <QPainter> 
#include <QPaintEvent> 

InkSpot::InkSpot(QWidget *parent) : 
    QWidget(parent) 
{ 
} 
void InkSpot::paintEvent(QPaintEvent *event) 
{ 
    QFile *brushInput; //takes raw 8 bit grayscale image, 8 bit values only 
    char *brushProto; 
    uchar *brushData; 

    brushInput = new QFile("x:\\Development\\InkPuppet\\brush.raw"); //open the raw file 
    brushInput->open(QIODevice::ReadOnly); 
    QDataStream in; 
    in.setDevice(brushInput); 
    int size = brushInput->size(); //set size to length of raw file 

    brushProto = new char[size]; 
    in.readRawData(brushProto, size); //read file into prototype 
    brushData = new uchar[size]; 

    for(int i = 0; i < size; ++i) 
    { 
     brushData[i] = (uchar)brushProto[i]; //copy char to uchar array 
    } 

    QImage test(brushData, 128, 128, QImage::Format_Indexed8); 
    QImage test2(128, 128, QImage::Format_ARGB32); 

    QVector<QRgb> vectorColors(256); //create color table 
    for(int c = 0; c < 256; c++) 
    { 
     vectorColors[c] = qRgb(c, c, c); 
    } 

    test.setColorTable(vectorColors); 

    for(int iX = 0; iX < 100; ++iX) 
    { 
     for(int iY = 0; iY < 100; ++iY) 
     { 
      test2.setPixel(iX, iY, qRgba(255 - (qrand() % 100), 0 + (qrand() % 100), 0 + (qrand() % 100), qAbs((int)test.pixel(iX, iY)-255))); 
     } 
    } 

    //final conversion for stencil and color brush 
    QPixmap testPixmap = QPixmap::fromImage(test2); 
    QPixmap testPixmap2 = QPixmap::fromImage(test); 

    QPainter painter(this); 

    painter.drawPixmap(150, 50, 100, 100, testPixmap); 
    painter.drawPixmap(50, 50, 100, 100, testPixmap2); 

    delete[] brushProto; 
    delete[] brushData; 
    delete brushInput; 
} 
+0

对于初学者,你会想'qDebug()'(在'#include'''后面)。这就是说,我不确定'InkSpot'是否是可打印的类型。如果它来自'QObject',你可能会询问它是'metaobject'([QMetaObject](http://qt-project.org/doc/qt-5.0/qtcore/qmetaobject.html))。另请参阅[QDebug](http://qt-project.org/doc/qt-5.0/qtcore/qdebug.html)参考。 – jpm

+0

谢谢,这似乎已经完成了这个技巧,不幸的是它只返回对象的内存地址,不太确定如何获取其他信息。我正在尝试这样做,因为每次我对InkSpot中的小部件执行任何操作时,程序都会崩溃。我将用更多的代码进行编辑。 – Vii

+0

我误传了最后一部分,它发生在我对InkSpot类内部的任何事情做任何事情时发生。 – Vii

回答

4

不是你想要做的小事。该qDebug()显示一些有用的东西,你必须实现'运营商< <'。下面是一个粗略的例子,它的外观如下:

QDebug inline operator<<(QDebug d, const InkSpot &f){ 
     QDebug nsp = d.nospace(); 
     nsp << f.label->text(); 
     nsp << "\n"; 
     return d; 
} 

这将打印InkSpot的标签文本。添加您需要的任何其他信息。将上面的代码放在InkSpot.h文件中,但在类定义之外。如果您需要访问InkSpots私人数据,您必须让QDebug运营商< <成为InkSpot的朋友。

相关问题