2

У таблицы два столбца. Можно ли как-то растянуть второй на всю оставшуюся длину?

Я заметил, что под названиями столбцов (под Date и Title) можно писать, можно ли например уменьшить высоту этих двух ячеек и убрать возможность вводить туда текст?


P.S.: Таблица для дальнейшего добавления строк. Если они выйдут за границу экрана, появится ли скролер ?

Заранее большое спасибо.


Фронт

from PySide2 import QtCore, QtGui, QtWidgets

class Ui_NoteList(object):
    def setupUi(self, NoteList):
        NoteList.setObjectName("NoteList")
        NoteList.resize(418, 597)
        self.centralwidget = QtWidgets.QWidget(NoteList)
        self.centralwidget.setObjectName("centralwidget")
        self.label = QtWidgets.QLabel(self.centralwidget)
        self.label.setGeometry(QtCore.QRect(50, 10, 81, 31))
        font = QtGui.QFont()
        font.setFamily("Consolas")
        font.setPointSize(20)
        self.label.setFont(font)
        self.label.setObjectName("label")
        self.label_2 = QtWidgets.QLabel(self.centralwidget)
        self.label_2.setGeometry(QtCore.QRect(10, 50, 121, 31))
        font = QtGui.QFont()
        font.setFamily("Consolas")
        font.setPointSize(20)
        self.label_2.setFont(font)
        self.label_2.setObjectName("label_2")
        self.line_login = QtWidgets.QLineEdit(self.centralwidget)
        self.line_login.setGeometry(QtCore.QRect(140, 10, 151, 31))
        font = QtGui.QFont()
        font.setFamily("Consolas")
        font.setPointSize(20)
        self.line_login.setFont(font)
        self.line_login.setObjectName("line_login")
        self.line_password = QtWidgets.QLineEdit(self.centralwidget)
        self.line_password.setGeometry(QtCore.QRect(140, 50, 151, 31))
        font = QtGui.QFont()
        font.setFamily("Consolas")
        font.setPointSize(20)
        self.line_password.setFont(font)
        self.line_password.setObjectName("line_password")
        self.button_log = QtWidgets.QPushButton(self.centralwidget)
        self.button_log.setGeometry(QtCore.QRect(300, 10, 111, 31))
        font = QtGui.QFont()
        font.setFamily("Consolas")
        font.setPointSize(16)
        self.button_log.setFont(font)
        self.button_log.setObjectName("button_log")
        self.button_sign = QtWidgets.QPushButton(self.centralwidget)
        self.button_sign.setGeometry(QtCore.QRect(300, 50, 111, 31))
        font = QtGui.QFont()
        font.setFamily("Consolas")
        font.setPointSize(16)
        self.button_sign.setFont(font)
        self.button_sign.setObjectName("button_sign")
        self.button_add = QtWidgets.QPushButton(self.centralwidget)
        self.button_add.setGeometry(QtCore.QRect(10, 90, 121, 31))
        font = QtGui.QFont()
        font.setFamily("Consolas")
        font.setPointSize(16)
        self.button_add.setFont(font)
        self.button_add.setObjectName("button_add")
        self.button_delete = QtWidgets.QPushButton(self.centralwidget)
        self.button_delete.setGeometry(QtCore.QRect(140, 90, 151, 31))
        font = QtGui.QFont()
        font.setFamily("Consolas")
        font.setPointSize(16)
        self.button_delete.setFont(font)
        self.button_delete.setObjectName("button_delete")
        self.button_edit = QtWidgets.QPushButton(self.centralwidget)
        self.button_edit.setGeometry(QtCore.QRect(300, 90, 111, 31))
        font = QtGui.QFont()
        font.setFamily("Consolas")
        font.setPointSize(16)
        self.button_edit.setFont(font)
        self.button_edit.setObjectName("button_edit")
        self.table_w = QtWidgets.QTableWidget(self.centralwidget)
        self.table_w.setGeometry(QtCore.QRect(10, 130, 401, 441))
        self.table_w.setObjectName("table_w")
        self.table_w.setColumnCount(3)
        self.table_w.setRowCount(0)
        item = QtWidgets.QTableWidgetItem()
        self.table_w.setHorizontalHeaderItem(0, item)
        item = QtWidgets.QTableWidgetItem()
        self.table_w.setHorizontalHeaderItem(1, item)
        item = QtWidgets.QTableWidgetItem()
        self.table_w.setHorizontalHeaderItem(2, item)
        NoteList.setCentralWidget(self.centralwidget)
        self.statusbar = QtWidgets.QStatusBar(NoteList)
        self.statusbar.setObjectName("statusbar")
        NoteList.setStatusBar(self.statusbar)

        self.retranslateUi(NoteList)
        QtCore.QMetaObject.connectSlotsByName(NoteList)

    def retranslateUi(self, NoteList):
        NoteList.setWindowTitle(QtWidgets.QApplication.translate("NoteList", "MainWindow", None, -1))
        self.label.setText(QtWidgets.QApplication.translate("NoteList", "LOGIN", None, -1))
        self.label_2.setText(QtWidgets.QApplication.translate("NoteList", "PASSWORD", None, -1))
        self.button_log.setText(QtWidgets.QApplication.translate("NoteList", "LOG IN", None, -1))
        self.button_sign.setText(QtWidgets.QApplication.translate("NoteList", "SIGN UP", None, -1))
        self.button_add.setText(QtWidgets.QApplication.translate("NoteList", "ADD", None, -1))
        self.button_delete.setText(QtWidgets.QApplication.translate("NoteList", "DELETE", None, -1))
        self.button_edit.setText(QtWidgets.QApplication.translate("NoteList", "EDIT", None, -1))
        self.table_w.horizontalHeaderItem(0).setText(QtWidgets.QApplication.translate("NoteList", "Date", None, -1))
        self.table_w.horizontalHeaderItem(1).setText(QtWidgets.QApplication.translate("NoteList", "Title", None, -1))
        self.table_w.horizontalHeaderItem(2).setText(QtWidgets.QApplication.translate("NoteList", "Text", None, -1))

Сам код

class Main(QtWidgets.QMainWindow, Ui_NoteList):

    LOGGED = False

    def __init__(self, parent=None):
        super(Main, self).__init__(parent)
        self.setupUi(self)

if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    win = Main()
    win.show()
    sys.exit(app.exec_())

1 ответ 1

2

Вы не используете макеты, поэтому предлагаю не растягивать, а установите ширину нужного вам столбца

self.table_w.setColumnWidth(1, 330)

Действия, которые будут инициировать редактирование элемента: https://doc.qt.io/qt-5/qabstractitemview.html#EditTrigger-enum

self.table.setEditTriggers(QtWidgets.QAbstractItemView.NoEditTriggers)

import sys
from PyQt5 import QtCore, QtGui, QtWidgets
#from PySide2 import QtCore, QtGui, QtWidgets


class Ui_NoteList(object):
    def setupUi(self, NoteList):
        NoteList.setObjectName("NoteList")
        NoteList.resize(418, 597)

        self.centralwidget = QtWidgets.QWidget(NoteList)
        self.centralwidget.setObjectName("centralwidget")

        self.label = QtWidgets.QLabel(self.centralwidget)
        self.label.setGeometry(QtCore.QRect(50, 10, 81, 31))
        font = QtGui.QFont()
        font.setFamily("Consolas")
        font.setPointSize(20)
        self.label.setFont(font)
        self.label.setObjectName("label")

        self.label_2 = QtWidgets.QLabel(self.centralwidget)
        self.label_2.setGeometry(QtCore.QRect(10, 50, 121, 31))
        font = QtGui.QFont()
        font.setFamily("Consolas")
        font.setPointSize(20)
        self.label_2.setFont(font)
        self.label_2.setObjectName("label_2")

        self.line_login = QtWidgets.QLineEdit(self.centralwidget)
        self.line_login.setGeometry(QtCore.QRect(140, 10, 151, 31))
        font = QtGui.QFont()
        font.setFamily("Consolas")
        font.setPointSize(20)
        self.line_login.setFont(font)
        self.line_login.setObjectName("line_login")

        self.line_password = QtWidgets.QLineEdit(self.centralwidget)
        self.line_password.setGeometry(QtCore.QRect(140, 50, 151, 31))
        font = QtGui.QFont()
        font.setFamily("Consolas")
        font.setPointSize(20)
        self.line_password.setFont(font)
        self.line_password.setObjectName("line_password")

        self.button_log = QtWidgets.QPushButton(self.centralwidget)
        self.button_log.setGeometry(QtCore.QRect(300, 10, 111, 31))
        font = QtGui.QFont()
        font.setFamily("Consolas")
        font.setPointSize(16)
        self.button_log.setFont(font)
        self.button_log.setObjectName("button_log")

        self.button_sign = QtWidgets.QPushButton(self.centralwidget)
        self.button_sign.setGeometry(QtCore.QRect(300, 50, 111, 31))
        font = QtGui.QFont()
        font.setFamily("Consolas")
        font.setPointSize(16)
        self.button_sign.setFont(font)
        self.button_sign.setObjectName("button_sign")

        self.button_add = QtWidgets.QPushButton(self.centralwidget)
        self.button_add.setGeometry(QtCore.QRect(10, 90, 121, 31))
        font = QtGui.QFont()
        font.setFamily("Consolas")
        font.setPointSize(16)
        self.button_add.setFont(font)
        self.button_add.setObjectName("button_add")

        self.button_delete = QtWidgets.QPushButton(self.centralwidget)
        self.button_delete.setGeometry(QtCore.QRect(140, 90, 151, 31))
        font = QtGui.QFont()
        font.setFamily("Consolas")
        font.setPointSize(16)
        self.button_delete.setFont(font)
        self.button_delete.setObjectName("button_delete")

        self.button_edit = QtWidgets.QPushButton(self.centralwidget)
        self.button_edit.setGeometry(QtCore.QRect(300, 90, 111, 31))
        font = QtGui.QFont()
        font.setFamily("Consolas")
        font.setPointSize(16)
        self.button_edit.setFont(font)
        self.button_edit.setObjectName("button_edit")

        self.table_w = QtWidgets.QTableWidget(self.centralwidget)  
        self.table_w.setGeometry(QtCore.QRect(10, 130, 401, 441))
        self.table_w.setObjectName("table_w")
        self.table_w.setColumnCount(3)
        self.table_w.setRowCount(0)
        item = QtWidgets.QTableWidgetItem()
        self.table_w.setHorizontalHeaderItem(0, item)
        item = QtWidgets.QTableWidgetItem()
        self.table_w.setHorizontalHeaderItem(1, item)
        item = QtWidgets.QTableWidgetItem()
        self.table_w.setHorizontalHeaderItem(2, item)


        NoteList.setCentralWidget(self.centralwidget)
        self.statusbar = QtWidgets.QStatusBar(NoteList)
        self.statusbar.setObjectName("statusbar")
        NoteList.setStatusBar(self.statusbar)

        self.retranslateUi(NoteList)
        QtCore.QMetaObject.connectSlotsByName(NoteList)

    def retranslateUi(self, NoteList):
        NoteList.setWindowTitle(QtWidgets.QApplication.translate("NoteList", "MainWindow", None, -1))
        self.label.setText(QtWidgets.QApplication.translate("NoteList", "LOGIN", None, -1))
        self.label_2.setText(QtWidgets.QApplication.translate("NoteList", "PASSWORD", None, -1))
        self.button_log.setText(QtWidgets.QApplication.translate("NoteList", "LOG IN", None, -1))
        self.button_sign.setText(QtWidgets.QApplication.translate("NoteList", "SIGN UP", None, -1))
        self.button_add.setText(QtWidgets.QApplication.translate("NoteList", "ADD", None, -1))
        self.button_delete.setText(QtWidgets.QApplication.translate("NoteList", "DELETE", None, -1))
        self.button_edit.setText(QtWidgets.QApplication.translate("NoteList", "EDIT", None, -1))
        self.table_w.horizontalHeaderItem(0).setText(QtWidgets.QApplication.translate("NoteList", "Date", None, -1))
        self.table_w.horizontalHeaderItem(1).setText(QtWidgets.QApplication.translate("NoteList", "Title", None, -1))
        self.table_w.horizontalHeaderItem(2).setText(QtWidgets.QApplication.translate("NoteList", "Text", None, -1))



class Main(QtWidgets.QMainWindow, Ui_NoteList):
    LOGGED = False   # ?

    def __init__(self, parent=None):
        super(Main, self).__init__(parent)
        self.setupUi(self)

        self.table_w.setRowCount(15)
        self.table_w.setColumnCount(3)
        self.table_w.horizontalHeader().setStretchLastSection(True)

        # Установите ширину нужного вам столбца
        self.table_w.setColumnWidth(0,  70)
        self.table_w.setColumnWidth(1, 280)    

        # Это свойство содержит политику переноса текста в элементе
        self.table_w.setWordWrap(False) 

        self.table_w.setEditTriggers(QtWidgets.QAbstractItemView.NoEditTriggers)
        self.table_w.setItem(0, 0, QtWidgets.QTableWidgetItem("12.12.3012")) 
        self.table_w.setItem(0, 1, QtWidgets.QTableWidgetItem("Редактирование невозможно."))        

        self.table_w.verticalHeader().setDefaultSectionSize(33)
        self.table_w.setAlternatingRowColors(True)


if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    win = Main()
    win.show()
    sys.exit(app.exec_())

введите сюда описание изображения

0

Ваш ответ

Нажимая «Отправить ответ», вы соглашаетесь с условиями пользования и подтверждаете, что прочитали политику конфиденциальности.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.