How to write qDebug
-
the question is:
*Using the function QObject::inherits(const char className), write qDebug statements testing if o inherits QObject and ValueObject.__
my code is:
widget.h
@
#ifndef WIDGET_H
#define WIDGET_H#include<QtGui>
#include <QtGui/QWidget>class Widget : public QWidget
{
Q_OBJECT
Q_PROPERTY(int _value READ value WRITE setValue)
public:
Widget(int val =0, QWidget *parent = 0);
void setValue(int val);
int value();
~Widget();
private:
QLabel *label;
int _value;
};#endif // WIDGET_H
@
widget.cpp
@
#include "widget.h"Widget::Widget(int val, QWidget *parent)
: QWidget(parent)
{
setValue(val);
label=new QLabel("I am liufan",this);QHBoxLayout *mainLayout=new QHBoxLayout(this); mainLayout->addWidget(label); this->setLayout(mainLayout); this->setWindowTitle(tr("liufan"));
}
Widget::~Widget()
{}
void Widget::setValue(int val)
{
if(val == _value){
return;
}
_value=val;
}int Widget::value()
{
return _value;
}@
main.cpp
@
#include <QtGui/QApplication>
#include "widget.h"int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
qDebug("Value: %d = %d", w.value(), w.property("_value").toInt());
w.setValue(42);
qDebug("Value: %d = %d", w.value(), w.property("_value").toInt());
w.setProperty("value", 11);
qDebug("Value: %d = %d", w.value(), w.property("_value").toInt());
w.show();return a.exec();
}
@
how do I to write the qDebug statements testing -
see "qInstallMsgHandler":http://doc.qt.nokia.com/latest/qtglobal.html#qInstallMsgHandler
-
First: It would be helpful and polite, if you would provide us with the information what's going wrong. It's quite unrespectful to just dump a bunch of code and have the folks here figure out themselves what's happening.
EDIT:
And I forgot: Choose your topic carefully! "how to write qDebug" has absolutely nothing to do with your actual problem!Regarding your problem:
try:
@
w.setProperty("_value", 11);
@ -
I am very sorry.my english is so poor.next I will be carefully to start a discussion
-
No problem with not so good English here, we're international, and not everybody speaks English well.
Can you explain a bit more detailed, what your problem is/was? I suspect that output should be
@
0 = 0
42 = 42
11 = 11
@But the last line reads 42 = 42. That's corrected by the fix I posted before.
-
yes,It's that teaching me how to understand the "Properties".because I am a beginer,so I'm not so clear what and how to do.thank you for helping me.
when commenting out the
@
//Q_PROPERTY(int _value READ value WRITE setValue)
@
I got
@
E:\study\qt\project\lianxi1-build-desktop\release\lianxi1.exe 启动中...
Value: 0 = 0
Value: 42 = 0
Value: 42 = 0
E:\study\qt\project\lianxi1-build-desktop\release\lianxi1.exe 退出, 代码: 0
@but
@
Q_PROPERTY(int _value READ value WRITE setValue)
@I got
@
E:\study\qt\project\lianxi1-build-desktop\release\lianxi1.exe 启动中...
Value: 0 = 0
Value: 42 = 42
Value: 42 = 42
E:\study\qt\project\lianxi1-build-desktop\release\lianxi1.exe 退出, 代码: 0
@thank you remind me that be careful to start a discussion
-
thank you for your tips,I will be more careful to do it.
I am a chinise undergraduate,I hope I can be a successful programmer. -
We all stumble over these tiny errors now and then :-)
Feel free to ask further questions - we do not bite (mostly :-) )
You might want to read http://www.catb.org/~esr/faqs/smart-questions.html some time, it has valuable hints on how to make a clever forum post.