封神榜温碧霞全集土豆:QTableWidget 表头以十六进制显示索引

来源:百度文库 编辑:中财网 时间:2024/05/05 11:40:42
QTableWidget 表头以十六进制显示索引

做的项目中有一项功能是显示抓下的数据包的内容,由于想要UltraEdit打开包的效果,所以打算在QT下把tableWidget里面的线隐藏掉,然后横纵表头都按照UE显示包的格式显示。
在QT中文论坛里搜了下,正好以前有人问过。贴下他们LZ问的和大家的回复。

请问QTableWidget 表头以十六进制显示索引怎么做?

管理提醒:
本帖被 XChinux 执行加亮操作(2008-11-30)
各位大虾
请问QTableWidget 表头以
十六进制显示索引怎么做?

6楼:
谢谢各位大虾,我用下面的代码实现了。
QString str;
for(i=0;i<16;i++){
tableWidget.setRowHeight(i,25);
str=QString("%1").arg(i,2,16,QLatin1Char('0')).toUpper();
tableWidget.setHorizontalHeaderItem(i,new QTableWidgetItem(str));
}

请问一下这里 “str=QString("%1").arg(i,2,16,QLatin1Char('0')).toUpper();”这一行,没有用new来分配 内存会不会有问题,另外可不可以不用定义str这个QString。
谢谢大家了。

8楼:
可以不定义的。调用tableWidget.setHorizontalHeaderItem(i,new QTableWidgetItem(str))的时候,会自动创建一个QString实例,然后把str的值拷贝过去。str在退出函数时自动被析构。

今天用的LZ自己提供的解决方法试的,果然是可以的。
以前以为arg()只是用来对应替换参数用的,没想到还有很多重载函数。
特意查了下文档,找到这里面对应以上方法的API

QString QString::arg ( int a, int fieldWidth = 0, int base = 10, const QChar & fillChar = QLatin1Char( ' ' ) ) const

This function overloads arg().

The a argument is expressed in base base, which is 10 by default and must be between 2 and 36. For bases other than 10, a is treated as an unsigned integer.

fieldWidth specifies the minimum amount of space that a is padded to and filled with the character fillChar. A positive value produces right-aligned text; a negative value produces left-aligned text.

The '%' can be followed by an 'L', in which case the sequence is replaced with a localized representation of a. The conversion uses the default locale, set by QLocale::setDefault(). If no default locale was specified, the "C" locale is used. The 'L' flag is ignored if base is not 10.

各参数的含义依次为第一个是一个int型证书,第二四个是对于前面的整数要添加fieldWidth个fillChar 第三个是指进制。这样一个for循环控制下就可以按照UE的格式显示了。

代码:
QString str1;
for(int i = 0;i < 103*16;i+=16){
str1 = QString("%1").arg(i,8,16,QLatin1Char('0'));
str1.append("h");
ui.tableWidget_14->setVerticalHeaderItem(i / 16,new QTableWidgetItem(str1));
}