• 6264阅读
  • 4回复

[提问]通过拖动表头,实现列内容的对换 [复制链接]

上一主题 下一主题
离线xsqxsy
 

只看楼主 正序阅读 楼主  发表于: 2012-09-19
     通过拖动表头,实现列内容的对换,下面的代码已经实现了,见图。
    然而我按表格的显示,依次读取每行每列的内容写入文本文件,但两个表格最后形成的文本文件却完全相同,而我真正需要的是: 表格二 按同样的方法读取,形成的文本内容是 文本二 的样式。那么下面的代码要怎样地改才能实现我想要的效果?

效果图如下:


代码如下:


  1. self.connect(self.table.horizontalHeader(), QtCore.SIGNAL("sectionMoved(int, int, int)"),self.reset_horizontal_header)
  2. .......
  3. def reset_horizontal_header(self,logicalIndex,oldVisualIndex,newVisualIndex):
  4.     header_texts = list ()
  5.     for x in range(self.table.columnCount()):
  6.         headItem = self.table.horizontalHeaderItem(x).text() #获得水平方向表头的Item对象
  7.         header_texts.append(headItem)
  8.     header = self.table.horizontalHeader()
  9.     for visual, text in enumerate(header_texts):
  10.         logic = header.logicalIndex(visual)
  11.         item = self.table.horizontalHeaderItem(logic)
  12.         item.setData(QtCore.Qt.DisplayRole, text)




离线xsqxsy

只看该作者 4楼 发表于: 2012-09-20
一直会等下去。
离线xsqxsy

只看该作者 3楼 发表于: 2012-09-19
没人能帮上忙吗?
离线xsqxsy

只看该作者 2楼 发表于: 2012-09-19

void QHeaderView::sectionMoved ( int logicalIndex, int oldVisualIndex, int n ewVisualIndex )  
  
上网查了一下这个信号的三个参数的含义如下:
  
logicalIndex 逻辑序号,不随列的移动而改变  
    
newVisualIndex 列位置变动后新的位置序号  
    
oldVisualIndex 列位置变动后上一次的位置序号(如果有的话)
____________________________________________________________________________________________________________________________________________________________________________________________
  
我初步猜想,既然拖动后,所拖动的那列的逻辑序号并示发生变化,解决的方法会不会是:只要把newVisualIndex列和oldVisualIndex列的逻辑序号相对换就可以了呢?如果真是这样需要用到什么样的方法呢?



离线xsqxsy

只看该作者 1楼 发表于: 2012-09-19
看了网上相关的一篇文章,还是没弄明白。
  
http://blog.chinaunix.net/uid-25806493-id-3341558.html
  
  
—————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————
  
  
QHeaderView
  
The QHeaderView class provides a header row or header column for item views.
  
A QHeaderView displays the headers used in item views such as the QTableView and QTreeView classes. It takes the place of Qt3's QHeader class previously used for the same purpose, but uses the Qt's model/view architecture for consistency with the item view classes.
  
The QHeaderView class is one of the Model/View Classes and is part of Qt's model/view framework.
  
The header gets the data for each section from the model using the QAbstractItemModel::headerData() function. You can set the data by using QAbstractItemModel::setHeaderData().
  
Each header has an orientation() and a number of sections, given by the count() function. A section refers to a part of the header - either a row or a column, depending on the orientation.
  
Sections can be moved and resized using moveSection() and resizeSection(); they can also be hidden and shown with hideSection() and showSection().
  
Each section of a header is described by a section ID, specified by its section(), and can be located at a particular visualIndex() in the header. A section can have a sort indicator set with setSortIndicator(); this indicates whether the items in the associated item view will be sorted in the order given by the section.
  
For a horizontal header the section is equivalent to a column in the model, and for a vertical header the section is equivalent to a row in the model.
  
Moving Header Sections
  
A header can be fixed in place, or made movable with setMovable(). It can be made clickable with setClickable(), and has resizing behavior in accordance with setResizeMode().
  
Note: Double-clicking on a header to resize a section only applies for visible rows.
  
A header will emit sectionMoved() if the user moves a section, sectionResized() if the user resizes a section, and sectionClicked() as well assectionHandleDoubleClicked() in response to mouse clicks. A header will also emit sectionCountChanged() and sectionAutoResize().
  
You can identify a section using the logicalIndex() and logicalIndexAt() functions, or by its index position, using the visualIndex() and visualIndexAt() functions. The visual index will change if a section is moved, but the logical index will not change.
  
Appearance
  
QTableWidget and QTableView create default headers. If you want the headers to be visible, you can use setVisible().
  
Not all ItemDataRoles will have an effect on a QHeaderView. If you need to draw other roles, you can subclass QHeaderView and reimplement paintEvent(). QHeaderView respects the following item data roles: TextAlignmentRole,  
  
DisplayRole, FontRole, DecorationRole, ForegroundRole, and BackgroundRole.
  
Note: Each header renders the data for each section itself, and does not rely on a delegate. As a result, calling a header's setItemDelegate() function will have no effect.
  
—————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————
  
  
Coordinate Systems
  
For some specialized forms of tables it is useful to be able to convert between row and column indexes and widget coordinates. The rowAt() function provides the y-coordinate within the view of the specified row; the row index can be used to obtain a corresponding y-coordinate with rowViewportPosition(). ThecolumnAt() and columnViewportPosition() functions provide the equivalent conversion operations between x-coordinates and column indexes.
  
快速回复
限100 字节
 
上一个 下一个