1.不顯示警告
只需要加載空回調(diào)函數(shù)即可
如下:
Bool cb(TidyDoc tdoc,TidyReportLevel lvl,uint line,uint col,ctmbstr mssg)
{
return no;
}
tidySetReportFilter(doc,(TidyReportFilter)cb);
2.顯示節(jié)點(diǎn)文本
TidyBuffer buf;
tidyBufInit(&buf);
tidyNodeGetText(doc,body,&buf);
printf("text:%s",buf.bp);
tidyBufFree(&buf);
使用msvc開發(fā)QT插件,不能簡單的使用vc向?qū)гO(shè)置為dll開發(fā),而應(yīng)該使用qmake或者選擇qt的vc插件
要不然出現(xiàn)的插件 在獲取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配合使用,高亮顯示關(guān)鍵字
一個簡單的例子如下:
#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();
當(dāng)然也可以 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();
出現(xiàn)這個錯誤的原因在于在次線程中執(zhí)行主線程對象的一些操作引起的
可以這樣修改
如果次線程需要更新主線程對象狀態(tài),需要發(fā)送消息,主線程對象接收后處理而不能在此線程中直接操作
默認(rèn)情況下QT是不支持中文的,一般在程序啟動的時候調(diào)用以下代碼,設(shè)置系統(tǒng)編碼方式即可
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
}
幾點(diǎn)說明:
1.setInternalDebuging 是啟用內(nèi)部調(diào)試日志,不過具體作用不是很明確
2.Appender是實(shí)現(xiàn)日志輸出的策略描述
一般情況下,參考以上例子就足夠了
#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設(shè)置文件內(nèi)容
3.從QDomDocument獲取文檔根節(jié)點(diǎn)
4.遍歷節(jié)點(diǎn)并作處理