英文:
How can I have a progress bar in a table widget spanned over two cells in PyQt5?
问题
I have a QTableWidget with 6 columns and 6 rows (the first row is a header row). It's a stock level 2 window where the headers are ask count, ask volume, ask, bid, bid volume, and bid count.
我有一个带有6列和6行的QTableWidget(第一行是标题行)。这是一个股票Level 2窗口,其中标题分别是ask count(卖出数量)、ask volume(卖出量)、ask(卖出价)、bid(买入价)、bid volume(买入量)和bid count(买入数量)。
I want to have two progress bars in each row based on the values of ask volume and bid volume, with the maximum value being the maximum of the ask and bid volume columns.
我希望每行都有两个进度条,基于ask volume(卖出量)和bid volume(买入量)的值,最大值是ask和bid volume列的最大值。
The first progress bar should span over the ask volume and ask price columns, and the second one should span over the bid price and bid volume columns. I want the ask count and bid count columns to be aligned center, ask volume to be left-aligned, and ask price to be right-aligned. Similarly, the bid price should be left-aligned, and bid volume should be right-aligned.
第一个进度条应跨越ask volume(卖出量)和ask price(卖出价)列,第二个进度条应跨越bid price(买入价)和bid volume(买入量)列。我希望ask count(卖出数量)和bid count(买入数量)列居中对齐,ask volume(卖出量)左对齐,ask price(卖出价)右对齐。类似地,bid price(买入价)应左对齐,bid volume(买入量)应右对齐。
I can do all the work, but the problem is that when I use the command tableWidget.setCellWidget(0,1, progress_bar)
to insert a progress bar in the corresponding cell, the text in the table gets replaced with the progress bar. To set the text, I have to use progress_bar.setFormat('{:,}.format()')
.
我可以完成所有工作,但问题是,当我使用命令 tableWidget.setCellWidget(0,1, progress_bar)
在相应的单元格中插入进度条时,表格中的文本会被进度条替换掉。要设置文本,我必须使用 progress_bar.setFormat('{:,}.format()')
。
However, when I want to span the progress bar over two cells using tableWidget.setSpan(0,1,1,2)
, I can't set the text of those cells separately so that I can adjust their alignment. The only solution I've found is to use progress_bar.setFormat('{:10,} {:6,}')
, but this doesn't allow me to adjust the alignment perfectly.
但是,当我想要使用 tableWidget.setSpan(0,1,1,2)
跨越两个单元格的方式设置进度条时,我无法单独设置这些单元格的文本,以便调整它们的对齐方式。我找到的唯一解决方案是使用 progress_bar.setFormat('{:10,} {:6,}')
,但这不能完美地调整对齐方式。
Here's an image of what I want to achieve:
以下是我想要实现的效果的图像:
How could I solve this?
我该如何解决这个问题?
英文:
I have a QTableWidget with 6 columns and 6 rows (the first row is a header row). It's a stock level 2 window where the headers are ask count, ask volume, ask, bid, bid volume, and bid count.
I want to have two progress bars in each row based on the values of ask volume and bid volume, with the maximum value being the maximum of the ask and bid volume columns.
The first progress bar should span over the ask volume and ask price columns, and the second one should span over the bid price and bid volume columns. I want the ask count and bid count columns to be aligned center, ask volume to be left-aligned, and ask price to be right-aligned. Similarly, the bid price should be left-aligned, and bid volume should be right-aligned.
I can do all the work, but the problem is that when I use the command tableWidget.setCellWidget(0,1, progress_bar)
to insert a progress bar in the corresponding cell, the text in the table gets replaced with the progress bar. To set the text, I have to use progress_bar.setFormat('{:,}.format()')
.
However, when I want to span the progress bar over two cells using tableWidget.setSpan(0,1,1,2)
, I can't set the text of those cells separately so that I can adjust their alignment. The only solution I've found is to use progress_bar.setFormat('{:10,} {:6,}'.format(ask_price,ask_volume))
, but this doesn't allow me to adjust the alignment perfectly.
Here's an image of what I want to achieve:
How could I solve this?
答案1
得分: 0
谢谢您的建议,@Carl HR,但我能够通过使用QStyledItemDelegate
的不同方法来克服问题,这种方法更容易。以下是我所做的:
-
首先,我定义了两个矩形,
left_rect
和right_rect
,分别表示合并单元格中左对齐和右对齐文本的区域。我使用了option.rect.adjusted(5, 0, 0, 0)
和option.rect.adjusted(0, 0, -5, 0)
方法来相应地调整矩形的尺寸。 -
接下来,我使用
QtCore.Qt.AlignLeft | QtCore.Qt.AlignVCenter
和QtCore.Qt.AlignRight | QtCore.Qt.AlignVCenter
的对齐值来设置左侧和右侧文本的所需对齐方式,分别存储在名为left_align
和right_align
的变量中。 -
最后,我使用
painter.drawText(left_rect, left_align, str(value))
方法在left_rect
区域内绘制左对齐文本,并在right_rect
区域内使用right_rect
和right_align
相似地绘制右对齐文本。
英文:
Thank you for the suggestions @Carl HR, but I was able to overcome the issue using a different approach with the QStyledItemDelegate
, which turned out to be much easier. Here's what I did:
-
First, I defined two rectangles,
left_rect
andright_rect
, to represent the areas for the left-aligned and right-aligned texts within the merged cells. I used theoption.rect.adjusted(5, 0, 0, 0)
andoption.rect.adjusted(0, 0, -5, 0)
methods to adjust the rectangles' dimensions accordingly. -
Next, I set the desired alignment for the left and right texts using the
QtCore.Qt.AlignLeft | QtCore.Qt.AlignVCenter
andQtCore.Qt.AlignRight | QtCore.Qt.AlignVCenter
alignment values, respectively. I stored them in variables namedleft_align
andright_align
. -
Finally, I used the
painter.drawText(left_rect, left_align, str(value))
method to draw the left-aligned text within theleft_rect
area, and similarly for the right-aligned text usingright_rect
andright_align
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论