如何在PyQt5中使表格小部件中的进度条跨足两个单元格?

huangapple go评论60阅读模式
英文:

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:

以下是我想要实现的效果的图像:

如何在PyQt5中使表格小部件中的进度条跨足两个单元格?

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:

如何在PyQt5中使表格小部件中的进度条跨足两个单元格?

How could I solve this?

答案1

得分: 0

谢谢您的建议,@Carl HR,但我能够通过使用QStyledItemDelegate的不同方法来克服问题,这种方法更容易。以下是我所做的:

  1. 首先,我定义了两个矩形,left_rectright_rect,分别表示合并单元格中左对齐和右对齐文本的区域。我使用了option.rect.adjusted(5, 0, 0, 0)option.rect.adjusted(0, 0, -5, 0)方法来相应地调整矩形的尺寸。

  2. 接下来,我使用QtCore.Qt.AlignLeft | QtCore.Qt.AlignVCenterQtCore.Qt.AlignRight | QtCore.Qt.AlignVCenter的对齐值来设置左侧和右侧文本的所需对齐方式,分别存储在名为left_alignright_align的变量中。

  3. 最后,我使用painter.drawText(left_rect, left_align, str(value))方法在left_rect区域内绘制左对齐文本,并在right_rect区域内使用right_rectright_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:

  1. First, I defined two rectangles, left_rect and right_rect, to represent the areas for the left-aligned and right-aligned texts within the merged cells. I used the option.rect.adjusted(5, 0, 0, 0) and option.rect.adjusted(0, 0, -5, 0) methods to adjust the rectangles' dimensions accordingly.

  2. Next, I set the desired alignment for the left and right texts using the QtCore.Qt.AlignLeft | QtCore.Qt.AlignVCenter and QtCore.Qt.AlignRight | QtCore.Qt.AlignVCenter alignment values, respectively. I stored them in variables named left_align and right_align.

  3. Finally, I used the painter.drawText(left_rect, left_align, str(value)) method to draw the left-aligned text within the left_rect area, and similarly for the right-aligned text using right_rect and right_align.

huangapple
  • 本文由 发表于 2023年5月11日 05:04:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/76222541.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定