1.不显示警告
只需要加载空回调函数即可
如下:
Bool cb(TidyDoc tdoc,TidyReportLevel lvl,uint line,uint col,ctmbstr mssg)
{
return no;
}
tidySetReportFilter(doc,(TidyReportFilter)cb);
2.显示节点文本
TidyBuffer buf;
tidyBufInit(&buf);
tidyNodeGetText(doc,body,&buf);
printf("text:%s",buf.bp);
tidyBufFree(&buf);
使用msvc开发QT插件,不能简单的使用vc向导设置为dll开发,而应该使用qmake或者选择qt的vc插件
要不然出现的插件 在获取intance()的时候会是空值
#include <QThread>
#include <QMutex>
#include <QWaitCondition>
#include <QQueue>
#include <QVariant>
#include <iostream>
const int DataSize = 100;
const int BufferSize = 20;
QQueue<QVariant> queue;
QWaitCondition bufferIsNotFull;
QWaitCondition bufferIsNotEmpty;
QMutex mutex;
QMutex printMutex;
void print(int flag,int var)
{
printMutex.lock();
if(flag == 0)
std::cout<<"set:"<<var<<std::endl;
else
std::cout<<"get:"<<var<<std::endl;
printMutex.unlock();
}
class Producer : public QThread
{
public:
void run();
};
void Producer::run()
{
for(int i = 0; i < DataSize; ++i)
{
mutex.lock();
while(queue.size() == BufferSize)
bufferIsNotFull.wait(&mutex);
int var = std::rand()%10;
print(0,var);
queue.append(var);
bufferIsNotEmpty.wakeAll();
mutex.unlock();
}
}
class Consumer : public QThread
{
public:
void run();
};
void Consumer::run()
{
while(1)
{
mutex.lock();
while(queue.isEmpty())
bufferIsNotEmpty.wait(&mutex);
int var = queue.dequeue().toInt();
print(1,var);
bufferIsNotFull.wakeAll();
mutex.unlock();
}
}
int main()
{
Producer producer1;
Consumer consumer1;
Producer producer2;
Consumer consumer2;
producer1.start();
consumer1.start();
producer2.wait();
consumer2.wait();
system("pause");
return 0;
}
QT中QSyntaxHighlighter主要和QTextEdit配合使用,高亮显示关键字
一个简单的例子如下:
#ifndef HIGHLIGHTER_H
#define HIGHLIGHTER_H
#include <QSyntaxHighlighter>
#include <QTextCharFormat>
QT_BEGIN_NAMESPACE
class QTextDocument;
QT_END_NAMESPACE
class Highlighter : public QSyntaxHighlighter
{
Q_OBJECT
public:
Highlighter(QTextDocument *parent = 0);
public slots:
void setTextQueue(const QStringList& textQueue);
protected:
void highlightBlock(const QString &text);
private:
struct HighlightingRule
{
QRegExp pattern;
QTextCharFormat format;
};
QVector<HighlightingRule> highlightingRules;
QTextCharFormat keywordFormat;
};
#endif
.cpp
#include <QtGui>
#include "highlighter.h"
Highlighter::Highlighter(QTextDocument *parent)
: QSyntaxHighlighter(parent)
{
HighlightingRule rule;
keywordFormat.setForeground(Qt::darkRed);
keywordFormat.setFontWeight(QFont::Bold);
}
void Highlighter::highlightBlock(const QString &text)
{
foreach(const HighlightingRule &rule,highlightingRules)
{
QRegExp expression(rule.pattern);
int index = expression.indexIn(text);
while(index >= 0)
{
int length = expression.matchedLength();
setFormat(index,length,rule.format);
index = expression.indexIn(text, index + length);
}
}
setCurrentBlockState(0);
}
void Highlighter::setTextQueue(const QStringList& textQueue)
{
highlightingRules.clear();
HighlightingRule rule;
const QString tmp("\\b");
foreach(const QString& str,textQueue)
{
QString pattern(tmp);
pattern += str;
pattern += tmp;
rule.pattern = QRegExp(pattern);
rule.format = keywordFormat;
highlightingRules.append(rule);
}
}
在这里setTextQueue是传入高亮显示的文本列表
1.QString to const char*
QString string;
const char* str = string.toLatin1.data();
当然也可以 const char* s = string.toStdString().c_str();
2.QByteArray to char*
QByteArray arrary;
char* ch = arrary.data();
3.QString to QByteArray
QString str;
QByteArray array = str.toUtf8();
出现这个错误的原因在于在次线程中执行主线程对象的一些操作引起的
可以这样修改
如果次线程需要更新主线程对象状态,需要发送消息,主线程对象接收后处理而不能在此线程中直接操作
默认情况下QT是不支持中文的,一般在程序启动的时候调用以下代码,设置系统编码方式即可
1 QTextCodec*codec= QTextCodec::codecForName("System");
2 QTextCodec::setCodecForLocale(codec);
3 QTextCodec::setCodecForCStrings(codec);
4 QTextCodec::setCodecForTr(codec);
5
简单的例子如下:
#include <stdio.h>
#include <ao/ao.h>
#include <sndfile.h>
#include <math.h>
int main(int argc, char **argv)
{
SNDFILE* infile = 0;
SF_INFO sfinfo;
int readcount;
int rate = 44100;
int channels = 2;
if(!(infile = sf_open("rock.wav",SFM_READ,&sfinfo)))
{
return 1 ;
};
rate = sfinfo.samplerate;
printf("rate %d\n",rate);
channels = sfinfo.channels;
printf("channels %d\n",channels);
printf("format %d\n",sfinfo.format);
short data[4096];
ao_device *device;
ao_sample_format format;
int default_driver;
int i;
ao_initialize();
default_driver = ao_default_driver_id();
memset(&format, 0, sizeof(format));
format.bits = 16;
format.channels = channels;
format.rate = rate;
format.byte_format = AO_FMT_LITTLE;
device = ao_open_live(default_driver,&format, NULL);
if(device == NULL)
{
fprintf(stderr,"error opening device.\n");
return 1;
}
while((readcount = sf_read_short(infile,data,4096)))
{
ao_play(device,data,readcount*2);
};
sf_close (infile) ;
ao_close(device);
ao_shutdown();
return (0);
}
输出到控制台
#include <iostream>
#include <log4cplus/helpers/loglog.h>
using namespace std;
using namespace log4cplus::helpers;
void print_message()
{
cout << "Entering print_message()" << endl;
LogLog::getLogLog()->debug(LOG4CPLUS_TEXT("This is a Debug statement"));
LogLog::getLogLog()->warn(LOG4CPLUS_TEXT("This is a Warning"));
LogLog::getLogLog()->error(LOG4CPLUS_TEXT("This is a Error"));
cout << "Exiting print_message()" << endl << endl;
}
int main()
{
print_message();
cout << "Turning on debug" << endl;
LogLog::getLogLog()->setInternalDebugging(true);
print_message();
cout << "Turning on quiet mode" << endl;
LogLog::getLogLog()->setQuietMode(true);
print_message();
system("pause");
return 0;
} 另外一个例子:
#include "log4cplus/logger.h"
#include "log4cplus/consoleappender.h"
#include "log4cplus/loglevel.h"
#include <iomanip>
using namespace std;
using namespace log4cplus;
int main()
{
SharedAppenderPtr append_1(new ConsoleAppender());
append_1->setName(LOG4CPLUS_TEXT("First"));
Logger::getRoot().addAppender(append_1);
Logger root = Logger::getRoot();
Logger test = Logger::getInstance(LOG4CPLUS_TEXT("test"));
LOG4CPLUS_DEBUG(root,
"This is"
<< " a reall"
<< "y long message." << endl
<< "Just testing it out" << endl
<< "What do you think?");
test.setLogLevel(NOT_SET_LOG_LEVEL);
LOG4CPLUS_DEBUG(test, "This is a bool: " << true);
LOG4CPLUS_INFO(test, "This is a char: " << 'x');
LOG4CPLUS_INFO(test, "This is a short: " << (short)-100);
LOG4CPLUS_INFO(test, "This is a unsigned short: " << (unsigned short)100);
LOG4CPLUS_INFO(test, "This is a int: " << (int)1000);
LOG4CPLUS_INFO(test, "This is a unsigned int: " << (unsigned int)1000);
LOG4CPLUS_INFO(test, "This is a long(hex): " << hex << (long)100000000);
LOG4CPLUS_INFO(test, "This is a unsigned long: " << (unsigned long)100000000);
LOG4CPLUS_WARN(test, "This is a float: " << (float)1.2345);
LOG4CPLUS_ERROR(test,
"This is a double: "
<< setprecision(15)
<< (double)1.2345234234);
LOG4CPLUS_FATAL(test,
"This is a long double: "
<< setprecision(15)
<< (long double)123452342342.342);
system("pause");
return 0;
}
再来看几个输出到日志的小例子:
#include <log4cplus/logger.h>
#include <log4cplus/fileappender.h>
#include <log4cplus/layout.h>
#include <log4cplus/ndc.h>
#include <log4cplus/helpers/loglog.h>
using namespace log4cplus;
const int LOOP_COUNT = 20000;
int main()
{
helpers::LogLog::getLogLog()->setInternalDebugging(true);
SharedAppenderPtr append_1(new RollingFileAppender(LOG4CPLUS_TEXT("Test.log"),5*1024,5));
append_1->setName(LOG4CPLUS_TEXT("LOG4PLUS"));
append_1->setLayout(std::auto_ptr<Layout>(new TTCCLayout()));
Logger::getRoot().addAppender(append_1);
Logger root = Logger::getRoot();
Logger test = Logger::getInstance(LOG4CPLUS_TEXT("test"));
Logger subTest = Logger::getInstance(LOG4CPLUS_TEXT("test.subtest"));
for(int i=0; i<LOOP_COUNT; ++i)
{
NDCContextCreator _context(LOG4CPLUS_TEXT("loop"));
LOG4CPLUS_DEBUG(subTest, "Entering loop #" << i);
}
return 0
}
几点说明:
1.setInternalDebuging 是启用内部调试日志,不过具体作用不是很明确
2.Appender是实现日志输出的策略描述
一般情况下,参考以上例子就足够了
#include <Qt/QFile.h>
#include <Qt/QDom.h>
#include <iostream>
void showNodeInfo(const QDomNode& node)
{
std::cout<<"node name:"<<node.nodeName().toStdString()<<std::endl;
QDomNode subnode = node.firstChild();
std::cout<<" node id:"<<subnode.nodeName().toStdString()<<std::endl;
std::cout<<" node id value:"<<subnode.toElement().text().toStdString()<<std::endl;
subnode = subnode.nextSibling();
std::cout<<" node name:"<<subnode.nodeName().toStdString()<<std::endl;
std::cout<<" node name value:"<<subnode.toElement().text().toStdString()<<std::endl;
subnode = subnode.nextSibling();
std::cout<<" node prefix:"<<subnode.nodeName().toStdString()<<std::endl;
std::cout<<" node prefix value:"<<subnode.toElement().text().toStdString()<<std::endl;
}
int main(int argc, char *argv[])
{
QDomDocument doc;
QFile xmlfile("config.xml");
if(!xmlfile.open(QIODevice::ReadOnly))return false;
if(doc.setContent(&xmlfile))
{
QDomElement root = doc.documentElement();
QDomNode node = root.firstChild();
for(;node.isNull()!=true;node=node.nextSibling())
showNodeInfo(node);
}
system("pause");
return 1;
}
基本步骤:
1.使用QFile载入文件
2.给QDomDocument设置文件内容
3.从QDomDocument获取文档根节点
4.遍历节点并作处理