學(xué)習(xí)QT的一個原因是貌似QT做出來的界面比較絢麗
我倒想看看能做出來啥樣子的
從QT窗體布局說起
凡是窗體布局無非就是如何擺放的問題
1.想當(dāng)然如果擺放有2個方式一個是所見即所得,一個是使用布局管理器
先說后者吧
2.QT有好幾種布局管理器無非就是啥子流式布局,格子布局等等
從這個層級上說軟件界面都是布局嵌套的
3.布局和控件的關(guān)系
一般是一個布局對應(yīng)于一個控件容器(或者頂層控件)
使用當(dāng)前布局管理器加掛子控件(容器)即可
然后給當(dāng)前控件掛上布局管理器即可
下面是一個簡單的QT Layout的例子(從QT例子改的)
class Dialog : public QDialog
{
Q_OBJECT
public:
Dialog();
private:
void createHorizontalGroupBox();
enum {button_number = 4};
QGroupBox *groupbox;
QPushButton *buttons[button_number];
QDialogButtonBox *buttonBox;
};
實現(xiàn)如下:
#include <QtGui>
#include "dialog.h"
//! [0]
Dialog::Dialog()
{
createHorizontalGroupBox();
buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok
| QDialogButtonBox::Cancel);
connect(buttonBox, SIGNAL(accepted()), this, SLOT(accept()));
connect(buttonBox, SIGNAL(rejected()), this, SLOT(reject()));
QVBoxLayout *mainLayout = new QVBoxLayout;
mainLayout->addWidget(groupbox);
mainLayout->addWidget(buttonBox);
setLayout(mainLayout);
setWindowTitle(tr("LayoutTest"));
}
void Dialog::createHorizontalGroupBox()
{
groupbox = new QGroupBox(tr("Layout Test"));
QHBoxLayout *layout = new QHBoxLayout;
buttons[0] = new QPushButton(tr("Button1"));
buttons[1] = new QPushButton(tr("Button2"));
buttons[2] = new QPushButton(tr("Button3"));
buttons[3] = new QPushButton(tr("Button4"));
for(int i = 0;i<button_number;i++)
layout->addWidget(buttons[i]);
groupbox->setLayout(layout);
}
幾個知識點:
1.groupbox
= new QGroupBox(tr
("Layout Test"));
Layout Test 是個文本這個無須解釋
那tr呢?查查資料知道是為了支持多語言
先知道即可以后使用的話在具體查查吧
2.QDialogButtonBox是個什么東西
看看最終的程序界面吧
原來是對話框的確認(rèn)和取消按鈕
再看信號槽函數(shù)無非就是綁定按鈕到操作函數(shù)
connect(buttonBox, SIGNAL(accepted()), this, SLOT(accept()));
connect(buttonBox, SIGNAL(rejected()), this, SLOT(reject()));
那accepted和accept函數(shù)有啥區(qū)別?
看看文檔
accept函數(shù)的解釋是:Hides the modal dialog and sets the result code to Accepted
accpeted函數(shù)的解釋是:This signal is emitted when the dialog has been accepted either
在說說QT皮膚
學(xué)習(xí)QT的主要目的就是想做做臉蛋好看好的軟件界面
那就試試看吧
查到的QT有一個名叫QSS(CSS?)的文件可以原來換膚
那就改改看吧
#include <QApplication>
#include <QFile>
#include <QStyleFactory>
#include <QTextStream>
#include "dialog.h"
void setSkin(QApplication* const app, QString const &skinFile);
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
setSkin(&app ,"skin.qss");
Dialog dialog;
dialog.show();
return app.exec();
}
void setSkin(QApplication* const app, QString const &skinFile)
{
QFile qss(skinFile);
qss.open(QFile::ReadOnly);
app->setStyleSheet(qss.readAll());
qss.close();
}
相應(yīng)的QSS文件如下:
QPushButton
{
color:red;
background:url(setting.png)
}
這里把PushButton的文本顏色設(shè)置為紅色
同時把它的背景設(shè)置為圖片stting.png
完了
PS:如果學(xué)習(xí)新知識?
囫圇吞棗比較適合快速學(xué)習(xí)
說來慚愧學(xué)習(xí)c++很長時間了一直沒有使用c++開發(fā)過軟件界面
所以現(xiàn)在想認(rèn)認(rèn)真真的學(xué)習(xí)一個c++圖形界面框架庫
本來想學(xué)習(xí)Xwidget但是這個資料不大好找 有啥問題不好解決
那就學(xué)習(xí)QT吧
不說QT的優(yōu)缺點,不說如何編譯QT
從QT的主要庫類開始吧
知道了基本的對象之后如果需要學(xué)習(xí)看看文檔就知道了
如果需要編譯QT的話再下個代碼試著編譯吧
QApplication 應(yīng)用程序類 管理圖形用戶界面應(yīng)用程序的控制流和主要設(shè)置
QLabel 標(biāo)簽類 提供文本或者圖像的顯示
QPushButton 按鈕類 提供了命令按鈕 按鈕的一種
QButtonGroup 按鈕組合類 按鈕組 相關(guān)按鈕的組合
QGroupBox 群組類 一個有標(biāo)題的組合框
QDateTimeEdit 日期時間編輯框類
QLineEdit 行編輯框類 單行文本編輯器
QTextEdit 文本編輯框類 單頁面多信息編輯器對象
QComboBox 組合框類
QProgressBar 進(jìn)度條類
QLCDNumber 數(shù)字顯示框類
QScrollBar 滾動條類
QSpinBox 微調(diào)框類
QSlider 滑動條類
QIconView 圖標(biāo)視圖類
QListView 列表視圖類
QListBox 列表框類
QTable 表格類
QValidator 有效性檢查類
QImage 圖像類
QMainWindow 主窗口類
QPopupMenu 彈出性菜單類
QMenuBar 菜單欄類
QToolButton 工具按鈕類
QToolTip 提示類
QWhatsThis 這是什么類
QAction 動作類
QHBoxLayout 水平布局類
QVBoxLayout 垂直布局類
QGridLayout 表格布局類
QT對話框類
QMessageBox 消息對話框類
QProgressDialog 進(jìn)度條對話框類
QWizard 向?qū)υ捒蝾?br>
QFileDialog 文件對話框類
QColorDialog 顏色對話框類
QFontDialog 字體對話框類
QPrintDialog 打印對話框類
基本就這些對象了
要系統(tǒng)學(xué)習(xí)QT 還需要看看QT的slot系統(tǒng),QT庫類接口等
具體的學(xué)習(xí)就是看例子咯
1.boost 這個使用的人多不多說了
2.pthread windows下的posix線程實現(xiàn)
3.libcurl 一個有名的開源網(wǎng)絡(luò)爬蟲庫 阿里旺旺中使用到了
4.libeay32 OpenSSL Library
5.libtidy 一個專門解析htm的庫
6.zlib 這個鬼都知道
7.freetype c接口的type2字體處理庫
8.libmad 一個編解碼mp3的庫
9.libogg,等 一個編解碼ogg音頻格式的庫
10.libsnd 一個開源的編解碼十多種音頻格式的庫
11.ffmpeg 一個關(guān)于音頻視頻處理的庫
12.Freeimage,Cximage,Devil 這3個都是用來處理圖形的庫
13.libpng,libjpeg,....基本同上
14.angelscript 一個類似lua的腳本引擎 其腳本風(fēng)格類似于標(biāo)準(zhǔn)c語言
15.flac/flac++一個編解碼flac音頻格式的庫
16.tinyxml,rapidxml,libxml 都是關(guān)于xml解析方面的
17.luaplus,luabind都是涉及綁定lua和c++的庫
18.ode,bullet 開源的物理引擎庫
19.timidity一個可以把mid音頻格式轉(zhuǎn)化為wav格式的庫
20.vlc一個類似ffmeg的庫
21.zthread一個類型boost-thread,pthread的c++風(fēng)格的多線程庫
22.sigc++,sigslot信號插槽庫 類型的有boost中的signal
23.SDL 簡單的音頻視頻庫
24.hge一個簡單的使用ddraw的2維游戲小引擎
25.opencv一個開源的處理圖形的庫
26.mygui,cegui 都是游戲上使用的GUI系統(tǒng)
27.鬼火游戲引擎,Orge,都是開源的游戲中間件
28.Wxwidget一個開源的跨平臺,類似MFC
29.QT ..
30.loki一個實驗性質(zhì)的c++庫
31.ace一個網(wǎng)絡(luò)通信庫
32.fmod一個有點名氣的游戲音效引擎
33.sqlite 一個開源的桌面數(shù)據(jù)庫
未完待續(xù)
libtidy是一個開源的用來診斷,分析,生成html文檔的一個庫
下面的例子是使用libtidy獲取頁面鏈接的例子
代碼如下:
#ifndef PARSEPAGE_HPP
#define PARFSPAGE_HPP
#include <string>
#include <vector>
#include <tidy/buffio.h>
#include <tidy/fileio.h>
#include <tidy/tidy.h>
#include <tidy/tidyenum.h>
#include <tidy/platform.h>
//! 解析html頁面
class ParsePage
{
public:
typedef std::vector<std::string> String;
public:
ParsePage(int rank = 0,const std::string& cur = ""):rank(rank),cur(cur)
{
doc = tidyCreate();
root = tidyGetRoot(doc);
}
~ParsePage()
{
tidyRelease(doc);
}
public:
//! 解析給定文件
bool LoadFile(const char* file)
{
return 1 == tidyParseFile(doc,file);
}
//!解析給定內(nèi)存
bool LoadBuffer(const char* buffer)
{
return 1 == tidyParseString(doc,buffer);
}
//! 內(nèi)容解析
void Check()
{
CheckHref(root);
}
//! 獲取鏈接
int GetLinkNumber()const{return links.size();}
std::string GetLinkByIndex(int index){return links.at(index);}
private:
void DoHref(TidyAttr attr);
void CheckHref(TidyNode node);
private:
TidyDoc doc;
TidyNode root;
std::string cur;
int rank;
String links;
};
#endif
//! ccsdu2004
實現(xiàn):
#include <boost/algorithm/string.hpp>
#include "parsepage.hpp"
void ParsePage::DoHref(TidyAttr attr)
{
std::string href(tidyAttrValue(attr));
//! 郵箱地址
if(boost::algorithm::starts_with(href,"mailto:"))
{
}
//! 鏈接地址
else
{
if(boost::algorithm::starts_with(href,"http:"))
{
size_t itr = href.find_last_of('#');
if(itr != std::string::npos)
{
href = href.substr(0,itr);
}
}
else
{
if(boost::algorithm::contains(href,"#"))
return;
}
links.push_back(href);
}
}
void ParsePage::CheckHref(TidyNode node)
{
TidyNode child;
for(child = tidyGetChild(node);child;child = tidyGetNext(child))
{
TidyAttr attr = tidyAttrGetHREF(child);
if(attr)
{
DoHref(attr);
}
CheckHref(child);
}
}
這個對象比較簡單
調(diào)用Check之后所有的頁面鏈接在links中
如何寫出高質(zhì)量的函數(shù)?
根據(jù)個人經(jīng)驗具體如下:
有遺漏請補充
1.從函數(shù)功能上考慮要求函數(shù)功能單一不能一個函數(shù)基本多個功能
2.從命名規(guī)則上考慮應(yīng)該變量,函數(shù)命名統(tǒng)一具體根據(jù)各個單位有所差異
3.從易讀性上考慮
一般函數(shù)應(yīng)該寫出函數(shù)描述,
為了能使函數(shù)簡單明了函數(shù)行數(shù)不宜太長以50行為宜
函數(shù)應(yīng)該以單一返回路徑為佳
4.從變量上考慮應(yīng)該盡可能使用局部變量而非全局變量
5.從函數(shù)健壯性上考慮函數(shù)應(yīng)該輸入?yún)?shù)是否為可能的合法值等等
6.從容錯性上考慮需要注意異常處理
7.另外還需要考慮函數(shù)中的變量是否可能會超出其表示范圍.
8.其他....
一道中興筆試題
要求是摳出給定字符串中的所有數(shù)字然后排序輸入
做法如下:
#include <cstdlib>
#include <iostream>
#include <string.h>
#include <ctype.h>
#include <algorithm>
#include <iterator>
using namespace std;
void output(char* str,int len)
{
if(str == NULL || len <= 0)
return;
int* data = (int*)malloc(len);
char* tmp = (char*)malloc(len+1);
memset(data,0,len);
memset(tmp,0,sizeof(char)*len);
int index = 0;
int i = 0;
int j = 0;
int flag = isdigit(str[0]);
while(1)
{
if(i==len || i+j == len+1)
break;
if(isdigit(str[i+j]) == 0 && flag == 0)
{
i=i+j+1;
j=0;
flag = isdigit(str[i+j]);
}
else if(isdigit(str[i+j]) == 0 && flag != 0)
{
memset(tmp,0,sizeof(char)*(len+1));
strncpy(tmp,str+i,j);
data[index++] = atoi(tmp);
flag = 0;
}
else
{
j++;
}
}
std::sort(data,data+index);
std::copy(data,data+index,std::ostream_iterator<int>(std::cout," "));
free(tmp);
free(data);
}
int main(int argc,char *argv[])
{
char input[] = "33k&99+r5sw1f10gd4vc511gc3";
output(input,strlen(input));
system("PAUSE");
return EXIT_SUCCESS;
}
嚴(yán)格說來不應(yīng)該使用stl中的函數(shù)和模板但是為了簡便起見還是這么寫吧
另外一直我一直己寫strcpyn函數(shù)用于復(fù)制給定字符串沒發(fā)現(xiàn)庫中有一個類型的strncpy函數(shù)
1.獲取錯誤 wavOutGetErrorText
static const char * mmerror(MMRESULT mmrError)
{
static char mmbuffer[1024];
int len;
sprintf(mmbuffer,"mm:%d ",(int)mmrError);
len = (int)strlen(mmbuffer);
waveOutGetErrorText(mmrError, mmbuffer+len, sizeof(mmbuffer)-len);
mmbuffer[sizeof(mmbuffer)-1] = 0;
return mmbuffer;
}
2.
檢取系統(tǒng)中存在的波形輸出設(shè)備的數(shù)量int wavmax = waveOutGetNumDevs();
3.
查詢一個指定的波形輸出設(shè)備以確定其性能
MMRESULT mmres = waveOutGetDevCaps(i, &caps, sizeof(caps));
if(mmres == MMSYSERR_NOERROR)
{
}
使用winmm播放音頻的例子具體可以參考:libhao具體請google.
4.
打開一個波形輸出設(shè)備
MMRESULT mmres;
mmres = waveOutOpen(&hwo,id,&wavefmt.Format,(DWORD_PTR)0,(DWORD_PTR)device,CALLBACK_NULL|WAVE_ALLOWSYNC);
if(mmres == MMSYSERR_NOERROR)
{
}
else
{
}
5.獲取波形輸出設(shè)備的標(biāo)識符
MMSYSERR_NOERROR == waveOutGetID(hwo,&id)
6.關(guān)閉波形輸出設(shè)備
waveOutClose(hwo)
7.設(shè)置,清除波形緩沖區(qū)
waveOutPrepareHeader
waveOutUnprepareHeader
8.向波形發(fā)送數(shù)據(jù)塊
mmres = waveOutWrite(hwo,&wh,sizeof(WAVEHDR));
為查詢函數(shù)如何使用最好的辦法就是使用google code
本著嚴(yán)肅,認(rèn)真的態(tài)度 打算開發(fā)新版的Gaimo Audio Library(版本一定要大于2.0.0)
打算重新書寫所有內(nèi)容
基本考慮如下:
1.不再使用openal音頻庫,也不打算使用Dsound代替之
而打算使用winmm(這樣接近底層,win32下)
2.不再聲稱支持各種格式的音頻文件而使用自定義音頻格式或者使用pcm或wav格式
3.新版SDK可能增加一個簡單的audio convertor以方便轉(zhuǎn)換音頻格式
4.新版SDK將增加打算的音頻音效算法(reverb,ring等.)
5.使用c++書寫,基于c借口以方便跨語言使用者
6.良好的跨平臺特性
7.以fmod為目標(biāo)
題外話:
如果你對這個感興趣可以聯(lián)系我以作為一個業(yè)余愛好
(僅僅業(yè)余的:-O)
FontForge是一個開源的工業(yè)字體庫
本文講述在win32下編譯他的流程
1.下載fontforge_full-20100501.tar.bz2
2.下載cygwin 安裝包
3.以默認(rèn)配置安裝cygwin
4.以默認(rèn)方式安裝的cygwin還缺少一些庫還需要安裝下列對象
x11,zlib,freetype等等
5.或者如果不知道那些包需要安裝那就全部安裝吧 雖然笨一點但是很有效!
6.解壓fontforge到cygwin下的prj目錄
7.點擊cygwin切換目錄到fontfogre下
8.執(zhí)行/configure
9.make install
10.等待n久
11.檢查編譯結(jié)果
這段時間沒咋編程序
就寫個c++排列組合函數(shù)的使用吧
以后使用得著的
#include <iostream>
#include <vector>
#include <algorithm>
#include <boost/assign.hpp>
#include <boost/function.hpp>
using namespace std;
using namespace boost;
using namespace boost::assign;
inline void print_(int t){cout<<t<<" ";}
inline void print(vector<int>& vec)
{
for_each(vec.begin(),vec.end(),print_);
cout<<endl;
}
//! 全排列測試
void test1()
{
vector<int> vec;
vec += 1,2,3,4,5,6,7,8;
sort(vec.begin(),vec.end());
int i = 0;
do
{
print(vec);
i++;
}
while(next_permutation(vec.begin(),vec.end()));
std::cout<<i<<std::endl;
}
//! 組合測試
size_t test2(int n,int m,boost::function<void(std::vector<int>& vec)> fn)
{
vector<int> p,set;
p.insert(p.end(),m,1);
p.insert(p.end(),n-m,0);
for(int i = 0;i != p.size();++i)
set.push_back(i+1);
vector<int> vec;
size_t cnt = 0;
do{
for(int i = 0;i != p.size();++i)
if(p[i])
vec.push_back(set[i]);
fn(vec);
cnt ++;
vec.clear();
}while(prev_permutation( p.begin(), p.end()));
return cnt;
}
int main()
{
test1();
std::cout<<test2(20,3,print)<<std::endl;
return 0;
}
....................................................................................................................