- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我想删除布局中表格中的空白区域:
我想要这样的东西:
这是我的代码:
import sys
from PyQt4 import QtGui
from PyQt4.QtGui import *
from PyQt4.QtSql import *
from PyQt4.QtCore import *
import sys
class Example(QMainWindow):
def __init__(self):
super(Example, self).__init__()
self.initUI()
def initUI(self):
self.resize(640, 480)
self.center()
self.setWindowTitle('')
self.setWindowIcon(QtGui.QIcon('icon.ico'))
exitAction = QtGui.QAction(QtGui.QIcon('exit.png'), '&Exit', self)
exitAction.setShortcut('Ctrl+Q')
exitAction.setStatusTip('Exit application')
exitAction.triggered.connect(QtGui.qApp.quit)
self.toolbar = self.addToolBar('Exit')
self.toolbar.addAction(exitAction)
menubar = self.menuBar()
fileMenu = menubar.addMenu('&File')
fileMenu.addAction(exitAction)
table = QTableWidget()
db = QSqlDatabase.addDatabase("")
db.setDatabaseName('xxx.db')
db.open()
query = QSqlQuery ("xxxx")
queryCount = QSqlQuery ("xxxx")
queryCount.next()
numberUserRow = queryCount.value(0).toInt()
table.setColumnCount(2)
table.setRowCount(numberUserRow[0])
table.setHorizontalHeaderItem(0, QTableWidgetItem("Post"));
table.setHorizontalHeaderItem(1, QTableWidgetItem("Data"));
MSG_POST = 3
DATA_SALVATAGGIO_POST = 4
index=0
while (query.next()):
table.setItem(index,0,QTableWidgetItem(query.value(MSG_POST).toString()))
table.setItem(index,1,QTableWidgetItem(query.value(DATA_SALVATAGGIO_POST).toString()))
index = index+1
self.statusBar().showMessage('Ready')
window = QWidget()
mainLayout = QGridLayout()
verticalLayout = QVBoxLayout()
horizontalLayout = QHBoxLayout()
horizontalLayout.addWidget(table)
verticalLayout.addLayout(horizontalLayout)
mainLayout.addLayout(verticalLayout,0,0)
window.setLayout(mainLayout)
self.setCentralWidget(window);
self.show()
def center(self):
qr = self.frameGeometry()
cp = QtGui.QDesktopWidget().availableGeometry().center()
qr.moveCenter(cp)
self.move(qr.topLeft())
def main():
app = QtGui.QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
我试着在我的 table 右边放一个按钮,这有助于我调整 table 本身的大小。但是我不需要使用按钮,那么如何固定表格的宽度呢?
最佳答案
最好的方法是在表格的右侧使用 QSpacerItem
。
horizontalSpacer = QtGui.QSpacerItem(20, 40, QtGui.QSizePolicy.Expanding, QSizePolicy.Expanding)
horizontalLayout.addWidget(table)
# Add the widget and the spacer to the horizontal layout
horizontalLayout.addItem(horizontalSpacer)
此外,您需要将小部件 Horizontal sizePolicy 设置为 Minimum,以去除表格中剩余的空白区域。为此,根据您的需要,有不同的选择。您可以将尺寸拉伸(stretch)至以下内容 this ,或放置一个固定尺寸,这可能是获得与您的图像最接近的结果的最佳方式:
table.setSizePolicy(QtGui.QSizePolicy.Minimum,QtGui.QSizePolicy.Expanding)
header = table.horizontalHeader()
table.setMaximumWidth(header.length())
table.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
显示结果here .
关于python - PyQt:删除 QTableWidget 中的空格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41967250/
有谁知道的返回值之间的关系 QTableWidget::item(x, y) 和 QTableWidget::cellWidget (x, y) 在同一个 QTableWidget 和同一个 row
当我将按钮拖放到 QTableWidget 中时,按钮会从旧位置消失,并且我放置按钮的单元格中不会显示任何内容。 有人可以提出问题是什么吗? 请在下面找到代码 import sys from PyQt
我有一个 QTableWidget,我想在其中填充另一个 QTableWidget 的字段。 这是它的样子: -------------------------- Name | Class | Sec
我问了一个与此非常相似的问题,但回答者提供了少量帮助,我无法解决我的问题。我想我会重新问这个问题,因为没有其他人回答我的问题。我想知道如何将 QTableWidget 的数据从一个窗口复制到另一个窗口
我的程序遇到了一个问题,我不知道如何解决。问题是我在两个类中有一个Qtablewidget,我使用信号和槽机制来传输一个QVector,它包含一个类的QTablewidgetItems,并将它放在其他
我有一个问题:调用 QTableWidget::update 不会导致 QTableWidget::paintEvent。 简要说明:- QTableWidgetEx - 派生自 QTableWidg
我正在使用 QTableWidget,当用户单击特定行时,我使用该索引来执行某些操作。我还使用 setSortEnable 启用了排序,所以当我通过单击标题对其进行排序时,所有行索引都会更改,但我想要
假设我有一个充满数据的二维数组,比如 10 x 10。内容以及行数可以随时更改。 现在我想在 QTableWidget 中显示这些数据。 我使用超时 1 秒的计时器来刷新表格内容。在超时槽中,如果我使
QTableWidget 有搜索用户数据行的方法吗? 类似的东西: //set user data row->setData(0, Qt::UserRole, "ID001"); //find row
我使用 UI 编辑器创建 QTableWidget。 MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui:
QTableWdiget对于简单的网格显示来说非常棒。更改颜色、字体等非常简单。 但是,我没有设法用更少的垂直空白来使网格看起来更“紧密”。我看到 Qt 文档讨论了(例如 here ) margin
可以为单元格设置占位符吗?我认为它可以通过为特定单元格设置 QLineEdit 来实现,但可能是 QTableWidget,或者 QTableWidgetItem 可以在没有自定义小部件的情况下做到这
当按下堆叠在左侧的按钮选择了 QTableWidget 中的整行时,是否有信号?我希望启用某些功能,但不确定如何启用? 提前致谢! 最佳答案 您有几个不同的选择。您所要求的最直接的方法是使用 QHea
在下面的代码中有两个样式表:problemStylesheet 和 okStylesheet。当我为 QTableWidget 使用 okStylesheet 时一切正常。标签在滚动。问题是如果我使用
QTableWidget由多个Selection Modi组成,可以通过方法进行选择 setSelectionMode (QAbstractItemView::SelectionMode mode)
我正在尝试使用最新的 Qt SDK (4.7.4) 来设置 QTableWidget 的样式。通过谷歌搜索,我发现我需要做这样的事情: QHeaderView::section { backg
我有一个 QTableWidget,我通过标题列使用它的默认排序功能,但我在 QTableWidget 中的一列是整数类型,并且通过 QTableWidget 默认排序它像字符串一样排序。所以有任何方
我有一个带有自定义小部件的 QTableWidget。这些小部件很大,几乎填满了整个滚动区域的高度,因此只有一行是可见的。 我可以用鼠标滚轮或拖动滚动条滚动,但行总是跳动。 有没有办法让QTableW
我尝试了 .setVerticalScrollMode(QtWidgets.QAbstractItemView.ScrollPerPixel) ,效果很好,但需要用户将鼠标移动到滚动条并使用它来体验平
我有一个带有自定义小部件的 QTableWidget。这些小部件很大,几乎填满了整个滚动区域的高度,因此只有一行是可见的。 我可以用鼠标滚轮或拖动滚动条滚动,但行总是跳动。 有没有办法让QTableW
我是一名优秀的程序员,十分优秀!