fontawesome-webfont.ttf can be download in font-awesome
iconhelper.h
#ifndef ICONHELPER_H
#define ICONHELPER_H
#include <QObject>
#include <QFont>
#include <QFontDatabase>
#include <QMutex>
#include <QLabel>
#include <QPushButton>
#include <QApplication>
#include <QTreeWidgetItem>
#define DEFAULT_ICON_SIZE 10
#define ICON_SIZE 12
class IconHelper : public QObject
{
public:
static IconHelper* Instance()
{
static QMutex mutex;
if(!_instance)
{
QMutexLocker locker(&mutex);
if(!_instance)
{
_instance = new IconHelper;
}
}
return _instance;
}
void SetIcon(QLabel* lab, QChar c, int size = DEFAULT_ICON_SIZE);
void SetIcon(QPushButton* btn, QChar c, int size = DEFAULT_ICON_SIZE);
private:
explicit IconHelper(QObject *parent = 0);
QFont iconFont;
static IconHelper* _instance;
};
#endif // ICONHELPER_H
iconhelper.cpp
#include "iconhelper.h"
IconHelper* IconHelper::_instance = 0;
IconHelper::IconHelper(QObject *) :
QObject(qApp)
{
int fontId = QFontDatabase::addApplicationFont(":/image/fontawesome/fontawesome-webfont.ttf");
QString fontName = QFontDatabase::applicationFontFamilies(fontId).at(0);
iconFont = QFont(fontName);
}
void IconHelper::SetIcon(QLabel *lab, QChar c, int size)
{
iconFont.setPointSize(size);
lab->setFont(iconFont);
lab->setText(c);
}
void IconHelper::SetIcon(QPushButton *btn, QChar c, int size)
{
iconFont.setPointSize(size);
btn->setFont(iconFont);
btn->setText(c);
}
checkbox.h
#ifndef CCHECKBOX_H
#define CCHECKBOX_H
#include <QWidget>
class QPushButton;
class QLabel;
enum ECheckBoxState {
ENoChecked,
EChecked
};
class CCheckBox : public QWidget
{
Q_OBJECT
public:
explicit CCheckBox(QString str,
int iWidth,
int iHeight,
QWidget *parent = 0);
void setChecked(bool b);
bool isChecked();
signals:
void sigStateChanged(int iState);
private slots:
void slot_btn_clicked(bool checked);
protected:
bool eventFilter(QObject *obj, QEvent *event);
private:
QPushButton * m_pBtn;
QLabel * m_pLabel;
int m_iCurState;
QString m_strStyle;
QString m_strHoverStyle;
QString m_strPressedStyle;
};
#endif // CCHECKBOX_H
checkbox.cpp
#include "checkbox.h"
#include <QPushButton>
#include <QLabel>
#include <QHBoxLayout>
#include "../common/iconhelper.h"
CCheckBox::CCheckBox(
QString str,
int iWidth,
int iHeight,
QWidget *parent) :
QWidget(parent)
{
m_pBtn = new QPushButton(this);
m_pLabel = new QLabel(this);
m_pBtn->setMinimumSize(iWidth, iHeight);
m_pBtn->setMaximumSize(iWidth, iHeight);
m_pBtn->setFlat(true);
m_pBtn->setCheckable(true);
m_pBtn->installEventFilter(this);
m_pLabel->setText(str);
m_pLabel->installEventFilter(this);
QHBoxLayout *layout = new QHBoxLayout();
layout->setContentsMargins(0, 0, 0, 0);
layout->addWidget(m_pBtn);
layout->addWidget(m_pLabel);
this->setLayout(layout);
m_strStyle = "QPushButton{border: 1px solid #B2B2B2; border-radius: 0; color: white; background-color: white;}"
"QPushButton:hover{border: 1px solid #278CDE; background-color: white;}"
"QPushButton:checked{}";
m_strHoverStyle =
"QPushButton{border: 1px solid #278CDE; border-radius: 0; color: white; background-color: white;}"
"QPushButton:hover{border: 1px solid #278CDE; background-color: white;}"
"QPushButton:checked{}";
m_strPressedStyle =
"QPushButton{border: 1px solid #278CDE; border-radius: 0; color: white; background-color: #278CDE;}"
"QPushButton:hover{border: 1px solid #278CDE;}"
"QPushButton:checked{}";
this->setStyleSheet(m_strStyle);
m_iCurState = ENoChecked;
IconHelper::Instance()->SetIcon(m_pBtn, QChar(), ICON_SIZE);
connect(m_pBtn, SIGNAL(clicked(bool)),
this, SLOT(slot_btn_clicked(bool)));
}
void
CCheckBox::setChecked(bool b)
{
if(b)
{
m_iCurState = EChecked;
this->setStyleSheet(m_strPressedStyle);
IconHelper::Instance()->SetIcon(m_pBtn, QChar(0xf00c), ICON_SIZE);
}
else
{
m_iCurState = ENoChecked;
this->setStyleSheet(m_strHoverStyle);
IconHelper::Instance()->SetIcon(m_pBtn, QChar(), ICON_SIZE);
}
}
bool
CCheckBox::isChecked()
{
return (bool)m_iCurState;
}
void
CCheckBox::slot_btn_clicked(bool /*checked*/)
{
if(m_iCurState == ENoChecked)
{
m_iCurState = EChecked;
this->setStyleSheet(m_strPressedStyle);
IconHelper::Instance()->SetIcon(m_pBtn, QChar(0xf00c), ICON_SIZE);
emit sigStateChanged((int)EChecked);
}
else if(m_iCurState == EChecked)
{
m_iCurState = ENoChecked;
this->setStyleSheet(m_strHoverStyle);
IconHelper::Instance()->SetIcon(m_pBtn, QChar(), ICON_SIZE);
emit sigStateChanged((int)ENoChecked);
}
}
bool
CCheckBox::eventFilter(QObject *obj, QEvent *event)
{
if(m_pLabel == qobject_cast<QLabel*>(obj) ||
m_pBtn == qobject_cast<QPushButton*>(obj))
{
if(event->type() == QEvent::MouseButtonPress)
{
slot_btn_clicked(false);
return true;
}
else if(event->type() == QEvent::Enter && m_iCurState == ENoChecked)
{
this->setStyleSheet(m_strHoverStyle);
return true;
}
else if(event->type() == QEvent::Leave && m_iCurState == ENoChecked)
{
this->setStyleSheet(m_strStyle);
return true;
}
}
return QObject::eventFilter(obj, event);
}
A: 0.0.0.0 ~ 127.255.255.255
B: 128.0.0.0 ~ 191.255.255.255
C: 192.0.0.0 ~ 223.255.255.255
A: 10.0.0.0 ~ 10.255.255.255
B: 172.16.0.0 ~ 172.31.255.255
C: 192.168.0.0 ~ 192.168.255.255
IP & Mask ==> 子网号 (不能全为0 或 全为1)
IP & (~Mask) ==> 主机号 (不能全为0 或 全为1)
Example1:
在 192.168.0.0 , 255.255.255.0 的网段中,
网络号是: 192.168.0.0
广播地址是: 192.168.0.255
主机地址是: 192.168.0.1 ~ 192.168.0.254
2^8 - 2 = 256 - 2 = 254 个主机地址。
Example2:
在 192.168.92.70 , 255.255.255.0 的网段中,
网络号是: 192.168.92.0
广播地址是: 192.168.92.255
主机地址是: 192.168.92.1 ~ 192.168.92.254
2^8 - 2 = 256 - 2 = 254 个主机地址。
Example3:
A网段: 192.168.1.2 , 255.255.255.128
B网段: 192.168.1.129 , 255.255.255.128
192.168.1.2 –> 192.168.1.00000010
192.168.1.129 –> 192.168.1.10000001
192.168.1.128 –> 192.168.1.10000000
因为A
和B
的前25位是不一样的,所以A
和B
是不同的网段,即子网号是不一样的。
checkip.h
#ifndef CHECKIP
#define CHECKIP
enum EIPMSG {
EVALID,
EINVALID_IPADDRESS,
EINVALID_IPMASK,
EINVALID_NETWORKID,
EINVALID_HOSTID,
EINVALID_LOOPBACK_ADDRESS,
EINVALID_NOT_ABC
};
void g_vIPDeleteZero(char *pc);
bool g_bIPAddressIsValid(const char *pcIPAddress, long long& llIp);
bool g_bIsIPmaskValid(const char *pcIPMask, long long& llMask);
int g_iIpAndMaskIsValid(const char * pcIPAddress, const char * pcIPMask);
bool g_bInTheSameLAN(const char * pcIPAddr1, const char * pcIPMask1,
const char * pcIPAddr2, const char * pcIPMask2);
#endif
checkip.cpp
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "checkip.h"
const int MIN_IP_LEN = 7;
const int MAX_IP_LEN = 15;
bool g_bIsValidIPString(const char * pc)
{
int iLen = strlen(pc);
if(iLen<MIN_IP_LEN || iLen>MAX_IP_LEN)
{
return false;
}
bool bCurrentIsDigit = false;
bool bCurrentIsDot = false;
int iNum = 0;
for (int i=0; i<iLen; i++)
{
if( isdigit(pc[i]) )
{
iNum = iNum*10 + (pc[i]-'0');
if(iNum > 255)
{
return false;
}
bCurrentIsDigit = true;
bCurrentIsDot = false;
}
else if( pc[i] == '.' )
{
if(!bCurrentIsDigit || bCurrentIsDot)
{
return false;
}
bCurrentIsDigit = false;
bCurrentIsDot = true;
iNum = 0;
}
else
{
return false;
}
}
if(bCurrentIsDot)
{
return false;
}
return true;
}
//example:
//Input: 00.007.000.01 Output: 0.7.0.1
void g_vIPDeleteZero(char *pc)
{
char ac[100] = {0};
strcpy(ac, pc);
int iLen = strlen(ac);
char *pcTemp = &ac[0];
char *pcFound = NULL;
int i;
int j;
for (i=0; pcTemp[i] != '\0'; i++)
{
if(pcTemp[i] == '0')
{
if((i+1<3) && isdigit(pcTemp[i+1]))
{
pcTemp[i] = 'f';
}
}
else
{
pcFound = strchr(pcTemp, '.');
if(pcFound != NULL)
{
pcTemp = pcFound + 1;
i = -1;
}
else
{
break;
}
}
}
for (i=0, j=0; i<iLen; )
{
if(ac[i] != 'f')
{
pc[j++] = ac[i++];
}
else
{
i++;
}
}
pc[j] = '\0';
}
void g_vIPchar2Int(const char *pc, long long & ll)
{
ll = 0;
char ac[1024] = {0};
strcpy(ac, pc);
int iTemp;
char acTemp[100] = {0};
char * pcHead = &ac[0];
char * pcFind = strchr(ac, '.');
while(pcFind != NULL)
{
*pcFind = '\0';
strcpy(acTemp, pcHead);
pcFind++;
pcHead = pcFind;
iTemp = atoi(acTemp);
ll = (ll + (long long)iTemp) * 256;
pcFind = strchr(pcHead, '.');
}
strcpy(acTemp, pcHead);
iTemp = atoi(acTemp);
ll = ll + (long long)iTemp;
}
bool g_bIPAddressIsValid(const char *pcIPAddress, long long& llIp)
{
if( !g_bIsValidIPString(pcIPAddress) )
{
return false;
}
g_vIPchar2Int(pcIPAddress, llIp);
if((llIp==0) || (llIp==0xffffffff))
{
return false;
}
else
{
return true;
}
}
bool g_bIsIPmaskValid(const char *pcIPMask, long long& llMask)
{
if( !g_bIsValidIPString(pcIPMask) )
{
return false;
}
g_vIPchar2Int(pcIPMask, llMask);
if((llMask|(llMask-1)) == 0xffffffff)
{
return true;
}
else
{
return false;
}
}
int g_iIpAndMaskIsValid(const char * pcIPAddress, const char * pcIPMask)
{
long long llIp = 0;
long long llMask = 0;
if(!g_bIPAddressIsValid(pcIPAddress, llIp))
{
return EINVALID_IPADDRESS;
}
if(!g_bIsIPmaskValid(pcIPMask, llMask))
{
return EINVALID_IPMASK;
}
int iHead = (llIp & 0xff000000) >> 24;
if(iHead == 127)
{
return EINVALID_LOOPBACK_ADDRESS;
}
if( !(iHead>0 && iHead<=223) )
{
return EINVALID_NOT_ABC;
}
long long llNet = llIp & llMask;
if(llNet==0 || llNet==llMask)
{
return EINVALID_NETWORKID;
}
long long llHost = llIp & (~llMask);
if(llHost==0 || llHost==((~llMask)&0xffffff))
{
return EINVALID_HOSTID;
}
return EVALID;
}
bool g_bInTheSameLAN(const char * pcIPAddr1, const char * pcIPMask1,
const char * pcIPAddr2, const char * pcIPMask2)
{
long long llIPAddr1 = 0;
long long llIPMask1 = 0;
long long llIPAddr2 = 0;
long long llIPMask2 = 0;
if( (!g_bIPAddressIsValid(pcIPAddr1, llIPAddr1)) ||
(!g_bIsIPmaskValid(pcIPMask1, llIPMask1)) ||
(!g_bIPAddressIsValid(pcIPAddr2, llIPAddr2)) ||
(!g_bIsIPmaskValid(pcIPMask2, llIPMask2)) )
{
return false;
}
long long llNet1 = llIPAddr1 & llIPMask1;
long long llNet2 = llIPAddr2 & llIPMask2;
return llNet1 == llNet2;
}
typedef struct _SNode {
QString str;
}SNode;
1. SNode *pNode = (SNode*)malloc(sizeof(SNode));
这是错误的, 有问题, 要用new。
SNode *pNode = new SNode;
2.
SNode node;
memset(&node, 0, sizeof(node));
这也有问题,好像在结构体内有类,类不能使用memset.
3. memcpy 用在这用情况也有问题。
#include <iostream>
#include <ctime>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
const int MAXNUMBER = 10000000;
const int MINNUMBER = 1000;
int main()
{
//typedef long clock_t
clock_t lStartTime = clock();
srand(unsigned(time(0)));
freopen("data.txt", "w", stdout);
unsigned long ulData = 0;
for (int i=0; i<1000000; i++)
{
ulData = rand() % (MAXNUMBER - MINNUMBER + 1) + MINNUMBER;
cout << ulData << ' '; //output the data to data.txt
}
fclose(stdout);
freopen("time.txt", "w", stdout);
cout << "Elapsed time: " << double(clock() - lStartTime) / CLOCKS_PER_SEC
<< 's' << endl;
fclose(stdout);
return 0;
}
#include <iostream>
#include <map>
using namespace std;
struct cmp
{
bool operator()(const char * pc1, const char * pc2) const
{
return strcmp(pc1, pc2)<0;
}
};
int main()
{
map<char *, int, cmp> myMap;
cout<<myMap.size()<<endl;
int i;
for ( i=0 ; i<10; i++)
{
char * acStr = new char[50];
strcpy(acStr, "huangjian");
char acNum[50] = {0};
itoa(i, acNum, 10);
strcat(acStr, acNum);
cout<<acStr<<endl;
myMap.insert(std::pair<char *, int>(acStr, i));
}
cout<<myMap.size()<<endl;
map<char *, int, cmp>::iterator it;
char acF[50] = "huangjian9";
char * pc = acF;
it = myMap.find(pc);
if( myMap.end() == it )
{
cout<<"end"<<endl;
}
cout<<it->first<<" "<<it->second <<endl;
for ( it=myMap.begin(); it != myMap.end(); it++)
{
cout<<it->first<<" "<<it->second<<endl;
}
cout<<endl<<endl;
map<const char *, int, cmp> cMap;
cMap.insert(std::pair<const char *, int>("huangjian1", 1));
cMap.insert(std::pair<const char *, int>("huangjian2", 2));
cMap.insert(std::pair<const char *, int>("huangjian3", 3));
cMap.insert(std::pair<const char *, int>("huangjian4", 4));
cMap.insert(std::pair<const char *, int>("huangjian5", 5));
map<const char *, int, cmp>::iterator itc;
for (itc=cMap.begin(); itc != cMap.end(); itc++)
{
cout<<itc->first<<" "<<itc->second<<endl;
}
getchar();
return 0;
}