Введение:
У меня есть listWidget
, который содержит QListWidgetItem
(s). На каждой из них у меня есть 3 кнопки. «Закрыть» должно полностью уничтожить QListWidgetItem
, отложить нужно временно закрыть, но появится в следующий тайм-аут. Поэтому я должен сохранить его в списке или скрыть временно. Часть пользовательского интерфейса, созданная в Designer.
Изображение:
![enter image description here](https://i.stack.imgur.com/DrjQd.png)
Уже пробовал:
Я пробовал @pyqtSlot(QListWidgetItem) def on_itemClicked(self, item)
, но сигнал НЕ издается при нажатии на кнопки - только при нажатии на сам элемент. Даже если вы щелкнете по элементу, он закроется, но у него все еще будет «рамка виджета» QListWidgetItem
, и он по-прежнему доступен при нажатии. Таким образом, он НЕ закрывает QListWidgetItem
, но inst_pop_up.
Изображение:
![enter image description here](https://i.stack.imgur.com/ApopB.png)
Я нашел решение, используяeventFilter, но не понял, как правильно установить его в Python. Я попытался закрыть onePopUp от его кнопки «Закрыть» (раскомментируйте здесь, чтобы увидеть: self.pushButton_close.clicked.connect(self.close)
), но он также оставляет «рамку виджета». Экземпляр popupMain запускается в init класса MainWindow:
self.pop_up_main = popupMain()
Таймер, вызываемый в классе MainWindow:
self.pop_up_main.slot_add_new()
self.pop_up_main.show()
Проблема в onePopUp или popupMain
popup.py
import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.uic import loadUi
class onePopUp(QWidget):
def __init__(self):
counter = 0
super(onePopUp, self).__init__(parent=None)
loadUi('one_pop_up.ui', self)
# self.setAttribute(Qt.WA_DeleteOnClose, on=True)
# self.pushButton_close.clicked.connect(self.close)
def closeEvent(self, event):
print('onePopUp : close event')
# super().close()
class popupMain(QDialog):
def __init__(self):
super(popupMain, self).__init__(parent=None)
loadUi('master_pop_up.ui', self)
self.listWidget.itemPressed.connect(self.on_itemClicked)
# Set up the user interface from Designer.
self.pushButton_add_extra_record.clicked.connect(self.slot_add_new)
# self.installEventFilter()
self.list_of_postponed_items = list()
@pyqtSlot(QListWidgetItem)
def on_itemClicked(self, item):
print('in on_itemClicked')
print('item is {}'.format(item))
# i = self.listWidget.item(item)
self.listWidget.removeItemWidget(item)
# def eventFilter(self, object, event):
#
# if event:
# print(event, repr(event))
# if object == self.dateEdit_date_of_record and event.type() == QMouseEvent.MouseButtonDblClick:
# print('dblclick!!!')
# self.dateEdit_date_of_record.setDisabled(False)
# return super(DialogAddItemRecords, self).eventFilter(object, event)
def slot_add_new(self):
print('im in slot_add_new')
# Creating a new list widget item whose parent is the listwidget itself
# QListWidgetItem * listWidgetItem = new QListWidgetItem(ui->listWidget);
list_widget_item = QListWidgetItem(self.listWidget)
list_widget_item.setFlags(list_widget_item.flags() & ~Qt.ItemIsSelectable)
# list_widget_item.setFlags(list_widget_item.flags() & ~Qt.ItemIsSelectable)
# Adding the item to the listwidget
# ui->listWidget->addItem(listWidgetItem);
self.listWidget.addItem(list_widget_item)
# Creating an object of the designed widget which is to be added to the listwidget
# TheWidgetItem * theWidgetItem = new TheWidgetItem;
inst_pop_up = onePopUp()
inst_pop_up.timeEdit.setTime(QTime.currentTime().addSecs(-15 * 60))
inst_pop_up.timeEdit_2.setTime(QTime.currentTime())
# Making sure that the listWidgetItem has the same size as the TheWidgetItem
# listWidgetItem->setSizeHint(theWidgetItem->sizeHint());
list_widget_item.setSizeHint(inst_pop_up.frameSize())
# print(inst_pop_up.dockWidget.testAttribute())
inst_pop_up.label_header.setText('test')
# Finally adding the itemWidget to the list
# ui->listWidget->setItemWidget(listWidgetItem, theWidgetItem);
self.listWidget.setItemWidget(list_widget_item, inst_pop_up)
# self.listWidget.addItem(list_widget_item, inst_pop_up)
# def slot_delete_item(self):
# list_widget_item = QListWidgetItem(self.listWidget)
# removeItemWidget
# popup_list_widget = QListWidget()
# one_pop_up_1 = onePopUp()
# popup_list_widget.addItem(one_pop_up_1)
def popup_main():
app = QApplication(sys.argv)
window = popupMain()
window.show()
sys.exit(app.exec_())
if __name__ == '__main__':
popup_main()
// master_pop_up.ui
<ui version="4.0">
<class>Dialog</class>
<widget class="QDialog" name="Dialog">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>481</width>
<height>203</height>
</rect>
</property>
<property name="font">
<font>
<family>Segoe UI</family>
<pointsize>12</pointsize>
</font>
</property>
<property name="windowTitle">
<string>Dialog</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QPushButton" name="pushButton">
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string/>
</property>
<property name="icon">
<iconset>
<normaloff>icons/application-sidebar-list.png</normaloff>icons/application-sidebar-list.png</iconset>
</property>
<property name="iconSize">
<size>
<width>24</width>
<height>24</height>
</size>
</property>
<property name="flat">
<bool>true</bool>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="pushButton_add_extra_record">
<property name="sizePolicy">
<sizepolicy hsizetype="Minimum" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>Add extra record</string>
</property>
<property name="icon">
<iconset>
<normaloff>icons/plus.png</normaloff>icons/plus.png</iconset>
</property>
<property name="iconSize">
<size>
<width>24</width>
<height>24</height>
</size>
</property>
<property name="flat">
<bool>true</bool>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="pushButton_postpone_all">
<property name="text">
<string>Postpone all</string>
</property>
<property name="icon">
<iconset>
<normaloff>icons/alarm-clock-big_24_24.png</normaloff>icons/alarm-clock-big_24_24.png</iconset>
</property>
<property name="iconSize">
<size>
<width>24</width>
<height>24</height>
</size>
</property>
<property name="flat">
<bool>true</bool>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="pushButton_close_all">
<property name="text">
<string>Close all</string>
</property>
<property name="icon">
<iconset>
<normaloff>icons/cross.png</normaloff>icons/cross.png</iconset>
</property>
<property name="iconSize">
<size>
<width>24</width>
<height>24</height>
</size>
</property>
<property name="flat">
<bool>true</bool>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="pushButton">
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string/>
</property>
<property name="icon">
<iconset>
<normaloff>icons/gear.png</normaloff>icons/gear.png</iconset>
</property>
<property name="iconSize">
<size>
<width>24</width>
<height>24</height>
</size>
</property>
<property name="flat">
<bool>true</bool>
</property>
</widget>
</item>
</layout>
</item>
<item>
<widget class="QListWidget" name="listWidget">
<property name="acceptDrops">
<bool>false</bool>
</property>
<property name="sizeAdjustPolicy">
<enum>QAbstractScrollArea::AdjustToContents</enum>
</property>
<property name="editTriggers">
<set>QAbstractItemView::NoEditTriggers</set>
</property>
<property name="tabKeyNavigation">
<bool>true</bool>
</property>
<property name="showDropIndicator" stdset="0">
<bool>false</bool>
</property>
<property name="dragDropMode">
<enum>QAbstractItemView::NoDragDrop</enum>
</property>
<property name="defaultDropAction">
<enum>Qt::IgnoreAction</enum>
</property>
<property name="alternatingRowColors">
<bool>true</bool>
</property>
<property name="selectionMode">
<enum>QAbstractItemView::SingleSelection</enum>
</property>
<property name="horizontalScrollMode">
<enum>QAbstractItemView::ScrollPerItem</enum>
</property>
<property name="movement">
<enum>QListView::Static</enum>
</property>
<property name="isWrapping" stdset="0">
<bool>false</bool>
</property>
<property name="resizeMode">
<enum>QListView::Adjust</enum>
</property>
<property name="uniformItemSizes">
<bool>true</bool>
</property>
<property name="selectionRectVisible">
<bool>true</bool>
</property>
<property name="sortingEnabled">
<bool>true</bool>
</property>
</widget>
</item>
</layout>
</widget>
<resources/>
<connections/>
</ui>
// one_pop_up.ui
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Form</class>
<widget class="QWidget" name="Form">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>373</width>
<height>114</height>
</rect>
</property>
<property name="font">
<font>
<family>Segoe UI</family>
<pointsize>12</pointsize>
</font>
</property>
<property name="windowTitle">
<string>Form</string>
</property>
<property name="locale">
<locale language="English" country="Germany"/>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QFrame" name="frame">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="minimumSize">
<size>
<width>0</width>
<height>48</height>
</size>
</property>
<property name="styleSheet">
<string notr="true">font: 12pt "Segoe UI";</string>
</property>
<property name="frameShape">
<enum>QFrame::StyledPanel</enum>
</property>
<layout class="QHBoxLayout" name="horizontalLayout_2">
<item>
<widget class="QLabel" name="label_header">
<property name="font">
<font>
<family>Segoe UI</family>
<pointsize>12</pointsize>
<weight>50</weight>
<italic>false</italic>
<bold>false</bold>
</font>
</property>
<property name="styleSheet">
<string notr="true"/>
</property>
<property name="text">
<string>TextLabel</string>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer_3">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QPushButton" name="pushButton_postpone">
<property name="minimumSize">
<size>
<width>0</width>
<height>0</height>
</size>
</property>
<property name="text">
<string>Postpone</string>
</property>
<property name="icon">
<iconset>
<normaloff>icons/alarm-clock-blue_24_24.png</normaloff>icons/alarm-clock-blue_24_24.png</iconset>
</property>
<property name="iconSize">
<size>
<width>16</width>
<height>16</height>
</size>
</property>
<property name="autoDefault">
<bool>false</bool>
</property>
<property name="flat">
<bool>true</bool>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="pushButton_close">
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="font">
<font>
<family>Segoe UI</family>
<pointsize>12</pointsize>
<weight>50</weight>
<italic>false</italic>
<bold>false</bold>
</font>
</property>
<property name="text">
<string>Close</string>
</property>
<property name="icon">
<iconset>
<normaloff>icons/prohibition.png</normaloff>icons/prohibition.png</iconset>
</property>
<property name="iconSize">
<size>
<width>16</width>
<height>16</height>
</size>
</property>
<property name="flat">
<bool>true</bool>
</property>
</widget>
</item>
</layout>
</widget>
</item>
<item>
<widget class="QFrame" name="">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Minimum">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QLabel" name="label_23">
<property name="text">
<string>From</string>
</property>
</widget>
</item>
<item>
<widget class="QTimeEdit" name="timeEdit">
<property name="enabled">
<bool>true</bool>
</property>
<property name="wrapping">
<bool>false</bool>
</property>
<property name="frame">
<bool>true</bool>
</property>
<property name="readOnly">
<bool>true</bool>
</property>
<property name="buttonSymbols">
<enum>QAbstractSpinBox::NoButtons</enum>
</property>
<property name="keyboardTracking">
<bool>false</bool>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer_2">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeType">
<enum>QSizePolicy::Fixed</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>5</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QLabel" name="label">
<property name="text">
<string>to</string>
</property>
</widget>
</item>
<item>
<widget class="QTimeEdit" name="timeEdit_2">
<property name="enabled">
<bool>true</bool>
</property>
<property name="frame">
<bool>true</bool>
</property>
<property name="readOnly">
<bool>true</bool>
</property>
<property name="buttonSymbols">
<enum>QAbstractSpinBox::NoButtons</enum>
</property>
<property name="keyboardTracking">
<bool>false</bool>
</property>
<property name="showGroupSeparator" stdset="0">
<bool>false</bool>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QPushButton" name="pushButton_add">
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>Add</string>
</property>
<property name="icon">
<iconset>
<normaloff>icons/arrow-curve.png</normaloff>icons/arrow-curve.png</iconset>
</property>
<property name="iconSize">
<size>
<width>24</width>
<height>24</height>
</size>
</property>
<property name="flat">
<bool>true</bool>
</property>
</widget>
</item>
</layout>
</widget>
</item>
<item>
<widget class="Line" name="line">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
</widget>
</item>
</layout>
</widget>
<resources/>
<connections/>
</ui>
Что мне нужноделать?
- Я должен различать, с какой кнопки отправляется сигнал.
- закрыть (кнопка «Закрыть»)
- принять форму QListWidget временно(«Отложить»)
- вызов метода из другого класса MainWindow.add_record (# с периодом времени в этом
widgetItem
) (кнопка «Добавить запись»)
- Также я должен сделать откладывание (кнопка «Отложить все») и закрытие для всех (кнопка «Закрытие всех») на кнопках
popupMain
. Но я даже не знаю, что делать на одном предмете ... PS Я везде занимался серфингом, но не могу понять, как это сделать!