Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2653667
  • 博文数量: 416
  • 博客积分: 10220
  • 博客等级: 上将
  • 技术积分: 4193
  • 用 户 组: 普通用户
  • 注册时间: 2006-12-15 09:47
文章分类

全部博文(416)

文章存档

2022年(1)

2021年(1)

2020年(1)

2019年(5)

2018年(7)

2017年(6)

2016年(7)

2015年(11)

2014年(1)

2012年(5)

2011年(7)

2010年(35)

2009年(64)

2008年(48)

2007年(177)

2006年(40)

我的朋友

分类: C/C++

2009-10-15 10:00:48

说明:
1。 支持用户需要增加的控件
2。 布局通过QGridLayout来实现(类似html的Table)
3. 支持Url打开网页
4. 由于是测试,只关注功能的实现,没有进行封装,若是自定义皮肤,涉及到QMessageBox自带的slots问题,暂时还没有想到好的解决办法
 
/***************************/
main.cpp
 
#include
#include "immessagebox.h"
int main(int argc, char *argv[])
{
 QApplication a(argc, argv);
 ImMessageBox w;
 QLabel* pLink = new QLabel("google");
 pLink->setText("");
 QLabel* pLink2 = new QLabel("sina");
 pLink2->setText("
");
 w.addWidgetEx(pLink, 6, 1, Qt::AlignLeft, true);
 w.addWidgetExt(pLink2, 6, 2, 1, 3,Qt::AlignLeft,true);
 w.show(); return a.exec();
}
 
/***************************/
immessagebox.h
#ifndef IMMESSAGEBOX_H
#define IMMESSAGEBOX_H
#include
class ImMessageBox : public QMessageBox
{
 Q_OBJECT
public:
 ImMessageBox(QWidget *parent = 0);
 ImMessageBox( const QString & title, const QString & text, QWidget *parent=0);
 ~ImMessageBox();
 
void addWidgetEx ( QWidget * widget, int row, int column, Qt::Alignment alignment = 0, bool bLink=false );
 void addWidgetExt ( QWidget * widget, int fromRow, int fromColumn, int rowSpan, int columnSpan,
        Qt::Alignment alignment = 0, bool bLink=false );

public slots:
 void onOpenUrl(const QString &url);
};
#endif // IMMESSAGEBOX_H
 
 
/***************************/
immessagebox.cpp
 
#include
#include
#include "immessagebox.h"
ImMessageBox::ImMessageBox(QWidget *parent)
 : QMessageBox(QMessageBox::Warning, "", "", 0, parent, Qt::Sheet)
{
 setFixedWidth(200);
 QCheckBox dontPrompt("Do not prompt again", this);
 QCheckBox dontPrompt2("Do not prompt again8888888", this);
 QLineEdit* pEdit = new QLineEdit("Input Text");
 QPushButton* pAdd = new QPushButton("add");
 QPushButton* pSave = addButton(QMessageBox::Save);
 QPushButton* pOK = addButton(QMessageBox::Close);
 // 对Label不对换行的改进测试
QString newStr;
 QString str("ddddddddd0 ddddddddd1  12233445566778899中期苛啊是的法ccddeef3 fggddddd5 dddd4 ddd5555ddd7 dddddddd8 ddddddd9 ddddq ddddddww");
 while(str.length() > 20)
 {
  QChar c = str.at(20);
  bool bLN = c.isLetterOrNumber();
  if (!bLN)
  {
   newStr += str.left(20) + "\n";
   str.remove(0, 20) ;
  }
  else
  {
   QString tmpStr = str.left(20);
   int nPos = tmpStr.lastIndexOf(" ");
   nPos = nPos>0?nPos:20;
   newStr += str.left(nPos) + "\n";
   str.remove(0, nPos) ;
  }  
 }
 newStr += str;
 QLabel* pLabel = new QLabel(newStr);
 pLabel->setWordWrap(true);

 pLabel->setIndent(20);
 QSpacerItem *pSpacer = new QSpacerItem(40, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);
 QComboBox* pCombox = new QComboBox();
 pCombox->addItem("aaaa");
 pCombox->addItem("bbbb");
 pCombox->addItem("CCCC");
 dynamic_cast< QGridLayout *>(layout())->addWidget(pLabel, 0, 1, 1, 4);
 dynamic_cast< QGridLayout *>(layout())->addWidget(&dontPrompt, 2, 1,1,4);
 dynamic_cast< QGridLayout *>(layout())->addWidget(&dontPrompt2, 3, 1,1,2);
 dynamic_cast< QGridLayout *>(layout())->addWidget(pEdit, 3, 3,1,2);
 
 dynamic_cast< QGridLayout *>(layout())->addWidget(pCombox, 5, 1, 1, 4);
 
 dynamic_cast< QGridLayout *>(layout())->addItem(pSpacer, 8, 1);
 dynamic_cast< QGridLayout *>(layout())->addWidget(pAdd, 8, 2);
 dynamic_cast< QGridLayout *>(layout())->addWidget(pSave, 8, 3);
 dynamic_cast< QGridLayout *>(layout())->addWidget(pOK, 8, 4);
 // replace default icon
 QPixmap map("bmp.png");
 setIconPixmap(map);
}
ImMessageBox::ImMessageBox( const QString & title, const QString & text, QWidget *parent)
: QMessageBox(parent)
{
 setWindowTitle(title);
}
ImMessageBox::~ImMessageBox()
{
}
void ImMessageBox::onOpenUrl(const QString &url)
{
 bool bRet = QDesktopServices::openUrl(url);
 qDebug() << url;
}
void ImMessageBox::addWidgetEx ( QWidget * widget, int row, int column, Qt::Alignment alignment, bool bLink )
{
 dynamic_cast< QGridLayout *>(layout())->addWidget(widget, row, column, alignment);
 if (bLink)
 {
  connect(widget, SIGNAL(linkActivated(const QString &)), this, SLOT(onOpenUrl(const QString &)));
 }
}
void ImMessageBox::addWidgetExt ( QWidget * widget, int fromRow, int fromColumn, int rowSpan, int columnSpan, Qt::Alignment alignment, bool bLink )
{
 dynamic_cast< QGridLayout *>(layout())->addWidget(widget, fromRow, fromColumn, rowSpan, columnSpan, alignment);
 if (bLink)
 {
  connect(widget, SIGNAL(linkActivated(const QString &)), this, SLOT(onOpenUrl(const QString &)));
 }
}
 
改进后的调用方法:

void MainWindow::msgBoxTest()
{
 ImMessageBox msgBox(QMessageBox::Information, "msgTitle", "ssssContent", QMessageBox::NoButton, this);
 //ImMessageBox msgBox("msgTitle", this);
 QLabel* pLabel = new QLabel("ddddddddddddddddddddddddddd", this);
 msgBox.addWidget(pLabel, 0, 1, 1, 4);
 
 QCheckBox dontPrompt("Do not prompt again", this);
 QCheckBox dontPrompt2("Do not prompt again8888888", this);
 QLineEdit* pEdit = new QLineEdit("Input Text");
 QPushButton* pAdd = new QPushButton("add");
 QPushButton* pSave = msgBox.addButton(QMessageBox::Save);
 QPushButton* pOK = msgBox.addButton(QMessageBox::Close);
 
 QSpacerItem *pSpacer = new QSpacerItem(40, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);
 QSpacerItem *pSpacer2 = new QSpacerItem(40, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);
 QComboBox* pCombox = new QComboBox();
 pCombox->addItem("aaaa");
 pCombox->addItem("bbbb");
 pCombox->addItem("CCCC");
 msgBox.exec();
}
效果图:
 
 
 
改变QMessageBox的大小 :
class MyMessageBox : public QMessageBox {
protected:
void showEvent(QShowEvent* event) {
QMessageBox::showEvent(event);
setFixedSize(640, 480);
}
};
 
 
它还存在问题,就是text用的是Label,不能自动换行,用QTextEdit替换即可(当然QTextEdit要做一些属性上的个性,让它看起来象Label)
 

QString stringToLines(const QString &src, int _lenth)
{
 QString retStr;
 if (!src.isEmpty())
 { 
  QByteArray str = src.toLocal8Bit();
  while(str.length() > _lenth)
  {
   str = str.trimmed();
   bool bDouble = false;
   int nLen = _lenth;
   char c = str.at(nLen-1);
   char c1 = str.at(nLen);
   if (c1 &0x80 )
   {
    bDouble = true;
   }
   
   {
    QByteArray tmpStr = str.left(nLen);
    int nPos = nLen;
    if (!bDouble)
    {
     // when last word of string is char, find last space of string
     nPos = tmpStr.lastIndexOf(" ");
     nPos = nPos > 0 ? nPos : nLen;
    }
    else
    {
     // when last word of string is not char, get string byte length
     QString sStr = QString(tmpStr);
     QByteArray bStr = sStr.toLocal8Bit();
     nPos = bStr.length();
    }
    
    retStr += QString(str.left(nPos)) + "\n";
    str.remove(0, nPos) ;
   }  
  }//endw
  retStr += str;
 }
 else
 {
  //TGLOGFILE(("string is NULL"), ("string is NULL"));
 }
 return retStr;
}
阅读(2366) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~