拖放到 QPlainTextBox
Drag and Drop to QPlainTextBox
我正在尝试使用 Pyqt5 开发 ide。我想要的是将列表的内容拖到 TextBox 中。列表元素应该像按钮一样工作。例如,对于"Sum",printf(a + b);应该。
但它不允许我将 List 元素放入文本框。
我该怎么做?
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtGui import QIcon
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(1055, 814)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.verticalLayout_5 = QtWidgets.QVBoxLayout(self.centralwidget)
self.verticalLayout_5.setObjectName("verticalLayout_5")
self.verticalLayout_6 = QtWidgets.QVBoxLayout()
self.verticalLayout_6.setObjectName("verticalLayout_6")
self.plainTextEdit_3 = QtWidgets.QPlainTextEdit(self.centralwidget)
font = QtGui.QFont()
font.setPointSize(10)
font.setBold(False)
font.setWeight(50)
font.setStyleStrategy(QtGui.QFont.PreferDefault)
self.plainTextEdit_3.setFont(font)
self.plainTextEdit_3.setInputMethodHints(QtCore.Qt.ImhNone)
self.plainTextEdit_3.setLineWrapMode(QtWidgets.QPlainTextEdit.NoWrap)
self.plainTextEdit_3.setObjectName("plainTextEdit_3")
self.plainTextEdit_3.setAcceptDrops(True)
self.verticalLayout_6.addWidget(self.plainTextEdit_3)
self.verticalLayout_5.addLayout(self.verticalLayout_6)
self.line_3 = QtWidgets.QFrame(self.centralwidget)
self.line_3.setFrameShape(QtWidgets.QFrame.HLine)
self.line_3.setFrameShadow(QtWidgets.QFrame.Sunken)
self.line_3.setObjectName("line_3")
self.verticalLayout_5.addWidget(self.line_3)
self.tabWidget = QtWidgets.QTabWidget(self.centralwidget)
self.tabWidget.setMinimumSize(QtCore.QSize(754, 0))
font = QtGui.QFont()
font.setBold(False)
font.setItalic(False)
font.setWeight(50)
font.setKerning(True)
self.tabWidget.setFont(font)
self.tabWidget.setWhatsThis("")
self.tabWidget.setTabPosition(QtWidgets.QTabWidget.East)
self.tabWidget.setTabsClosable(True)
self.tabWidget.setObjectName("tabWidget")
self.tab = QtWidgets.QWidget()
self.tab.setObjectName("tab")
self.gridLayout = QtWidgets.QGridLayout(self.tab)
self.gridLayout.setObjectName("gridLayout")
self.tabWidget_2 = QtWidgets.QTabWidget(self.tab)
self.tabWidget_2.setEnabled(True)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Preferred)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.tabWidget_2.sizePolicy().hasHeightForWidth())
self.tabWidget_2.setSizePolicy(sizePolicy)
self.tabWidget_2.setTabPosition(QtWidgets.QTabWidget.North)
self.tabWidget_2.setUsesScrollButtons(True)
self.tabWidget_2.setObjectName("tabWidget_2")
self.tab_3 = QtWidgets.QWidget()
self.tab_3.setObjectName("tab_3")
self.gridLayout_6 = QtWidgets.QGridLayout(self.tab_3)
self.gridLayout_6.setObjectName("gridLayout_6")
self.pushButton = QtWidgets.QPushButton(self.tab_3)
self.pushButton.setObjectName("pushButton")
self.gridLayout_6.addWidget(self.pushButton, 0, 0, 1, 1)
self.checkableButton = QtWidgets.QPushButton(self.tab_3)
self.checkableButton.setCheckable(True)
self.checkableButton.setChecked(True)
self.checkableButton.setObjectName("checkableButton")
self.gridLayout_6.addWidget(self.checkableButton, 1, 0, 1, 1)
self.pushButton_5 = QtWidgets.QPushButton(self.tab_3)
self.pushButton_5.setObjectName("pushButton_5")
self.gridLayout_6.addWidget(self.pushButton_5, 2, 0, 1, 1)
self.pushButton_4 = QtWidgets.QPushButton(self.tab_3)
self.pushButton_4.setMinimumSize(QtCore.QSize(132, 50))
self.pushButton_4.setObjectName("pushButton_4")
self.gridLayout_6.addWidget(self.pushButton_4, 3, 0, 1, 1)
self.tabWidget_2.addTab(self.tab_3, "")
self.gridLayout.addWidget(self.tabWidget_2, 1, 0, 1, 1)
self.tabWidget.addTab(self.tab, "")
self.verticalLayout_5.addWidget(self.tabWidget)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 1055, 21))
self.menubar.setObjectName("menubar")
self.menuMenu = QtWidgets.QMenu(self.menubar)
self.menuMenu.setObjectName("menuMenu")
self.menuSubmenu_2 = QtWidgets.QMenu(self.menuMenu)
self.menuSubmenu_2.setObjectName("menuSubmenu_2")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.toolBar = QtWidgets.QToolBar(MainWindow)
self.toolBar.setObjectName("toolBar")
MainWindow.addToolBar(QtCore.Qt.TopToolBarArea, self.toolBar)
self.dockWidget1 = QtWidgets.QDockWidget(MainWindow)
self.dockWidget1.setMinimumSize(QtCore.QSize(150, 284))
self.dockWidget1.setObjectName("dockWidget1")
self.dockWidgetContents = QtWidgets.QWidget()
self.dockWidgetContents.setObjectName("dockWidgetContents")
self.verticalLayout_10 = QtWidgets.QVBoxLayout(self.dockWidgetContents)
self.verticalLayout_10.setObjectName("verticalLayout_10")
self.toolBox = QtWidgets.QToolBox(self.dockWidgetContents)
self.toolBox.setLayoutDirection(QtCore.Qt.LeftToRight)
self.toolBox.setObjectName("toolBox")
self.page = QtWidgets.QWidget()
self.page.setGeometry(QtCore.QRect(0, 0, 132, 211))
self.page.setObjectName("page")
self.gridLayout_2 = QtWidgets.QGridLayout(self.page)
self.gridLayout_2.setObjectName("gridLayout_2")
self.listWidget = QtWidgets.QListWidget(self.page)
self.listWidget.setObjectName("listWidget")
self.listWidget.setDragEnabled(True)
item = QtWidgets.QListWidgetItem()
self.listWidget.addItem(item)
item = QtWidgets.QListWidgetItem()
self.listWidget.addItem(item)
item = QtWidgets.QListWidgetItem()
self.listWidget.addItem(item)
item = QtWidgets.QListWidgetItem()
self.listWidget.addItem(item)
item = QtWidgets.QListWidgetItem()
self.listWidget.addItem(item)
item = QtWidgets.QListWidgetItem()
self.listWidget.addItem(item)
item = QtWidgets.QListWidgetItem()
self.listWidget.addItem(item)
self.gridLayout_2.addWidget(self.listWidget, 0, 0, 1, 1)
self.toolBox.addItem(self.page, "")
self.page_2 = QtWidgets.QWidget()
self.page_2.setGeometry(QtCore.QRect(0, 0, 132, 211))
self.page_2.setObjectName("page_2")
self.verticalLayout_2 = QtWidgets.QVBoxLayout(self.page_2)
self.verticalLayout_2.setObjectName("verticalLayout_2")
self.listWidget_2 = QtWidgets.QListWidget(self.page_2)
self.listWidget_2.setObjectName("listWidget_2")
item = QtWidgets.QListWidgetItem()
self.listWidget_2.addItem(item)
item = QtWidgets.QListWidgetItem()
self.listWidget_2.addItem(item)
item = QtWidgets.QListWidgetItem()
self.listWidget_2.addItem(item)
item = QtWidgets.QListWidgetItem()
self.listWidget_2.addItem(item)
self.verticalLayout_2.addWidget(self.listWidget_2)
self.verticalWidget = QtWidgets.QWidget(self.page_2)
self.verticalWidget.setObjectName("verticalWidget")
self.verticalLayout_3 = QtWidgets.QVBoxLayout(self.verticalWidget)
self.verticalLayout_3.setObjectName("verticalLayout_3")
self.verticalLayout_2.addWidget(self.verticalWidget, 0, QtCore.Qt.AlignTop)
self.toolBox.addItem(self.page_2, "")
self.page_7 = QtWidgets.QWidget()
self.page_7.setGeometry(QtCore.QRect(0, 0, 132, 211))
self.page_7.setObjectName("page_7")
self.verticalLayout_4 = QtWidgets.QVBoxLayout(self.page_7)
self.verticalLayout_4.setObjectName("verticalLayout_4")
self.verticalWidget1 = QtWidgets.QWidget(self.page_7)
self.verticalWidget1.setObjectName("verticalWidget1")
self.verticalLayout_7 = QtWidgets.QVBoxLayout(self.verticalWidget1)
self.verticalLayout_7.setObjectName("verticalLayout_7")
self.listWidget_3 = QtWidgets.QListWidget(self.verticalWidget1)
self.listWidget_3.setObjectName("listWidget_3")
item = QtWidgets.QListWidgetItem()
self.listWidget_3.addItem(item)
item = QtWidgets.QListWidgetItem()
self.listWidget_3.addItem(item)
item = QtWidgets.QListWidgetItem()
self.listWidget_3.addItem(item)
item = QtWidgets.QListWidgetItem()
self.listWidget_3.addItem(item)
item = QtWidgets.QListWidgetItem()
self.listWidget_3.addItem(item)
self.verticalLayout_7.addWidget(self.listWidget_3)
self.verticalLayout_4.addWidget(self.verticalWidget1, 0, QtCore.Qt.AlignTop)
self.toolBox.addItem(self.page_7, "")
self.verticalLayout_10.addWidget(self.toolBox)
self.dockWidget1.setWidget(self.dockWidgetContents)
MainWindow.addDockWidget(QtCore.Qt.DockWidgetArea(1), self.dockWidget1)
self.dockWidget2 = QtWidgets.QDockWidget(MainWindow)
self.dockWidget2.setObjectName("dockWidget2")
self.dockWidgetContents_2 = QtWidgets.QWidget()
self.dockWidgetContents_2.setObjectName("dockWidgetContents_2")
self.gridLayout_3 = QtWidgets.QGridLayout(self.dockWidgetContents_2)
self.gridLayout_3.setObjectName("gridLayout_3")
self.toolBox_2 = QtWidgets.QToolBox(self.dockWidgetContents_2)
self.toolBox_2.setObjectName("toolBox_2")
self.page_5 = QtWidgets.QWidget()
self.page_5.setGeometry(QtCore.QRect(0, 0, 132, 347))
self.page_5.setObjectName("page_5")
self.verticalLayout_8 = QtWidgets.QVBoxLayout(self.page_5)
self.verticalLayout_8.setObjectName("verticalLayout_8")
self.verticalWidget2 = QtWidgets.QWidget(self.page_5)
self.verticalWidget2.setObjectName("verticalWidget2")
self.verticalLayout_9 = QtWidgets.QVBoxLayout(self.verticalWidget2)
self.verticalLayout_9.setObjectName("verticalLayout_9")
self.pushButton_24 = QtWidgets.QPushButton(self.verticalWidget2)
self.pushButton_24.setObjectName("pushButton_24")
self.verticalLayout_9.addWidget(self.pushButton_24)
self.pushButton_25 = QtWidgets.QPushButton(self.verticalWidget2)
self.pushButton_25.setObjectName("pushButton_25")
self.verticalLayout_9.addWidget(self.pushButton_25)
self.pushButton_29 = QtWidgets.QPushButton(self.verticalWidget2)
self.pushButton_29.setObjectName("pushButton_29")
self.verticalLayout_9.addWidget(self.pushButton_29)
self.pushButton_30 = QtWidgets.QPushButton(self.verticalWidget2)
self.pushButton_30.setObjectName("pushButton_30")
self.verticalLayout_9.addWidget(self.pushButton_30)
self.pushButton_28 = QtWidgets.QPushButton(self.verticalWidget2)
self.pushButton_28.setObjectName("pushButton_28")
self.verticalLayout_9.addWidget(self.pushButton_28)
self.pushButton_27 = QtWidgets.QPushButton(self.verticalWidget2)
self.pushButton_27.setObjectName("pushButton_27")
self.verticalLayout_9.addWidget(self.pushButton_27)
self.pushButton_26 = QtWidgets.QPushButton(self.verticalWidget2)
self.pushButton_26.setObjectName("pushButton_26")
self.verticalLayout_9.addWidget(self.pushButton_26)
self.pushButton_23 = QtWidgets.QPushButton(self.verticalWidget2)
self.pushButton_23.setObjectName("pushButton_23")
self.verticalLayout_9.addWidget(self.pushButton_23)
self.verticalLayout_8.addWidget(self.verticalWidget2, 0, QtCore.Qt.AlignTop)
self.toolBox_2.addItem(self.page_5, "")
self.gridLayout_3.addWidget(self.toolBox_2, 0, 0, 1, 1)
self.dockWidget2.setWidget(self.dockWidgetContents_2)
MainWindow.addDockWidget(QtCore.Qt.DockWidgetArea(1), self.dockWidget2)
self.toolBar_2 = QtWidgets.QToolBar(MainWindow)
self.toolBar_2.setObjectName("toolBar_2")
MainWindow.addToolBar(QtCore.Qt.TopToolBarArea, self.toolBar_2)
self.actionAction = QtWidgets.QAction(MainWindow)
self.actionAction.setObjectName("actionAction")
self.actionSub_menu = QtWidgets.QAction(MainWindow)
self.actionSub_menu.setObjectName("actionSub_menu")
self.actionAction_C = QtWidgets.QAction(MainWindow)
self.actionAction_C.setObjectName("actionAction_C")
self.dockWidget1.raise_()
self.menuSubmenu_2.addAction(self.actionSub_menu)
self.menuSubmenu_2.addAction(self.actionAction_C)
self.menuMenu.addAction(self.actionAction)
self.menuMenu.addAction(self.menuSubmenu_2.menuAction())
self.menubar.addAction(self.menuMenu.menuAction())
self.toolBar.addAction(self.actionAction)
self.toolBar.addSeparator()
self.toolBar.addAction(self.actionSub_menu)
self.retranslateUi(MainWindow)
self.tabWidget.setCurrentIndex(0)
self.tabWidget_2.setCurrentIndex(0)
self.toolBox.setCurrentIndex(2)
self.toolBox_2.setCurrentIndex(0)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
MainWindow.setTabOrder(self.pushButton, self.checkableButton)
MainWindow.setTabOrder(self.checkableButton, self.pushButton_5)
MainWindow.setTabOrder(self.pushButton_5, self.tabWidget)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.plainTextEdit_3.setPlainText(_translate("MainWindow", "#include <stdlib.h>\n"
"\n"
"main(){\n"
" printf ( Hello Wolrd!\");\n"
" return 0;\n"
"}"))
self.pushButton.setText(_translate("MainWindow", "Compile"))
self.checkableButton.setText(_translate("MainWindow", "Compile and Run"))
self.pushButton_5.setText(_translate("MainWindow", "Run"))
self.pushButton_4.setText(_translate("MainWindow", "Değişken Yarat"))
self.tabWidget_2.setTabText(self.tabWidget_2.indexOf(self.tab_3), _translate("MainWindow", "Tab 1"))
self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab), _translate("MainWindow", "Tab 1"))
self.menuMenu.setTitle(_translate("MainWindow", "&Menu"))
self.menuSubmenu_2.setTitle(_translate("MainWindow", "&Submenu 2"))
self.toolBar.setWindowTitle(_translate("MainWindow", "toolBar"))
self.dockWidget1.setWindowTitle(_translate("MainWindow", "Yapılar"))
__sortingEnabled = self.listWidget.isSortingEnabled()
self.listWidget.setSortingEnabled(False)
item = self.listWidget.item(0)
item.setText(_translate("MainWindow", "Topla"))
item = self.listWidget.item(1)
item.setText(_translate("MainWindow", "Çıkar"))
item = self.listWidget.item(2)
item.setText(_translate("MainWindow", "Çarp"))
item = self.listWidget.item(3)
item.setText(_translate("MainWindow", "Böl"))
item = self.listWidget.item(4)
item.setText(_translate("MainWindow", "Üst Al"))
item = self.listWidget.item(5)
item.setText(_translate("MainWindow", "Kök Al"))
item = self.listWidget.item(6)
item.setText(_translate("MainWindow", "Mod Al"))
self.listWidget.setSortingEnabled(__sortingEnabled)
self.toolBox.setItemText(self.toolBox.indexOf(self.page), _translate("MainWindow", "İşlemler"))
__sortingEnabled = self.listWidget_2.isSortingEnabled()
self.listWidget_2.setSortingEnabled(False)
item = self.listWidget_2.item(0)
item.setText(_translate("MainWindow", "For"))
item = self.listWidget_2.item(1)
item.setText(_translate("MainWindow", "While"))
item = self.listWidget_2.item(2)
item.setText(_translate("MainWindow", "If"))
item = self.listWidget_2.item(3)
item.setText(_translate("MainWindow", "Switch / Case"))
self.listWidget_2.setSortingEnabled(__sortingEnabled)
self.toolBox.setItemText(self.toolBox.indexOf(self.page_2), _translate("MainWindow", "Döngüler"))
__sortingEnabled = self.listWidget_3.isSortingEnabled()
self.listWidget_3.setSortingEnabled(False)
item = self.listWidget_3.item(0)
item.setText(_translate("MainWindow", "Değer Al"))
item = self.listWidget_3.item(1)
item.setText(_translate("MainWindow", "Atama Yap"))
item = self.listWidget_3.item(2)
item.setText(_translate("MainWindow", "Çıktı Yaz"))
item = self.listWidget_3.item(3)
item.setText(_translate("MainWindow", "Kes"))
item = self.listWidget_3.item(4)
item.setText(_translate("MainWindow", "Devam Et"))
self.listWidget_3.setSortingEnabled(__sortingEnabled)
self.toolBox.setItemText(self.toolBox.indexOf(self.page_7), _translate("MainWindow", "Deyimler"))
self.dockWidget2.setWindowTitle(_translate("MainWindow", "Algoritmalar"))
self.pushButton_24.setText(_translate("MainWindow", "Quick Sort"))
self.pushButton_25.setText(_translate("MainWindow", "Merge Sort"))
self.pushButton_29.setText(_translate("MainWindow", "Radix Sort"))
self.pushButton_30.setText(_translate("MainWindow", "Insertion Sort"))
self.pushButton_28.setText(_translate("MainWindow", "Bubble Sort"))
self.pushButton_27.setText(_translate("MainWindow", "Counting Sort"))
self.pushButton_26.setText(_translate("MainWindow", "Heap Sort"))
self.pushButton_23.setText(_translate("MainWindow", "Selection Sort"))
self.toolBox_2.setItemText(self.toolBox_2.indexOf(self.page_5), _translate("MainWindow", "Sıralama Algoritması"))
self.toolBar_2.setWindowTitle(_translate("MainWindow", "toolBar_2"))
self.actionAction.setText(_translate("MainWindow", "&Action"))
self.actionSub_menu.setText(_translate("MainWindow", "&Action B"))
self.actionSub_menu.setToolTip(_translate("MainWindow", "submenu"))
self.actionAction_C.setText(_translate("MainWindow", "Action &C"))
import style_Dark_rc
import style_black_rc
import sys
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
ex = Ui_MainWindow()
w = QtWidgets.QMainWindow()
ex.setupUi(w)
w.show()
sys.exit(app.exec_())
解释:
QPlainTextEdit 仅支持纯文本,例如拖放信息,但在 QListWidget 的情况下,它使用另一个 mimeType 发送信息,因此失败。
解法:
有两种选择:向QPlaintTextedit 添加接受另一个mimeType 数据的功能,或者让信息源(在本例中为QListWidget)以纯文本形式发送信息。在这种情况下,我将实施后者,因为它是最简单的解决方案。
当信息通过拖放从一个元素发送到另一个元素时,使用 QMimeData,它通过源发送的事件和接收者接收的事件发送,在 QListWidget 的情况下它有将项目转换为 QMimeData 的方法 mimeData()
,以便可以通过添加纯文本来覆盖该方法。那么我们只在脚本中导入:
# ...
class ListWidget(QtWidgets.QListWidget):
def mimeData(self, items):
md = super().mimeData(items)
text = "".join([it.text() for it in items])
md.setText(text)
return md
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
# ...
self.gridLayout_2.setObjectName("gridLayout_2")
# self.listWidget = QtWidgets.QListWidget(self.page)
<b>self.listWidget = ListWidget(self.page)</b>
self.listWidget.setObjectName("listWidget")
# ...
我正在尝试使用 Pyqt5 开发 ide。我想要的是将列表的内容拖到 TextBox 中。列表元素应该像按钮一样工作。例如,对于"Sum",printf(a + b);应该。
但它不允许我将 List 元素放入文本框。
我该怎么做?
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtGui import QIcon
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(1055, 814)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.verticalLayout_5 = QtWidgets.QVBoxLayout(self.centralwidget)
self.verticalLayout_5.setObjectName("verticalLayout_5")
self.verticalLayout_6 = QtWidgets.QVBoxLayout()
self.verticalLayout_6.setObjectName("verticalLayout_6")
self.plainTextEdit_3 = QtWidgets.QPlainTextEdit(self.centralwidget)
font = QtGui.QFont()
font.setPointSize(10)
font.setBold(False)
font.setWeight(50)
font.setStyleStrategy(QtGui.QFont.PreferDefault)
self.plainTextEdit_3.setFont(font)
self.plainTextEdit_3.setInputMethodHints(QtCore.Qt.ImhNone)
self.plainTextEdit_3.setLineWrapMode(QtWidgets.QPlainTextEdit.NoWrap)
self.plainTextEdit_3.setObjectName("plainTextEdit_3")
self.plainTextEdit_3.setAcceptDrops(True)
self.verticalLayout_6.addWidget(self.plainTextEdit_3)
self.verticalLayout_5.addLayout(self.verticalLayout_6)
self.line_3 = QtWidgets.QFrame(self.centralwidget)
self.line_3.setFrameShape(QtWidgets.QFrame.HLine)
self.line_3.setFrameShadow(QtWidgets.QFrame.Sunken)
self.line_3.setObjectName("line_3")
self.verticalLayout_5.addWidget(self.line_3)
self.tabWidget = QtWidgets.QTabWidget(self.centralwidget)
self.tabWidget.setMinimumSize(QtCore.QSize(754, 0))
font = QtGui.QFont()
font.setBold(False)
font.setItalic(False)
font.setWeight(50)
font.setKerning(True)
self.tabWidget.setFont(font)
self.tabWidget.setWhatsThis("")
self.tabWidget.setTabPosition(QtWidgets.QTabWidget.East)
self.tabWidget.setTabsClosable(True)
self.tabWidget.setObjectName("tabWidget")
self.tab = QtWidgets.QWidget()
self.tab.setObjectName("tab")
self.gridLayout = QtWidgets.QGridLayout(self.tab)
self.gridLayout.setObjectName("gridLayout")
self.tabWidget_2 = QtWidgets.QTabWidget(self.tab)
self.tabWidget_2.setEnabled(True)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Preferred)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.tabWidget_2.sizePolicy().hasHeightForWidth())
self.tabWidget_2.setSizePolicy(sizePolicy)
self.tabWidget_2.setTabPosition(QtWidgets.QTabWidget.North)
self.tabWidget_2.setUsesScrollButtons(True)
self.tabWidget_2.setObjectName("tabWidget_2")
self.tab_3 = QtWidgets.QWidget()
self.tab_3.setObjectName("tab_3")
self.gridLayout_6 = QtWidgets.QGridLayout(self.tab_3)
self.gridLayout_6.setObjectName("gridLayout_6")
self.pushButton = QtWidgets.QPushButton(self.tab_3)
self.pushButton.setObjectName("pushButton")
self.gridLayout_6.addWidget(self.pushButton, 0, 0, 1, 1)
self.checkableButton = QtWidgets.QPushButton(self.tab_3)
self.checkableButton.setCheckable(True)
self.checkableButton.setChecked(True)
self.checkableButton.setObjectName("checkableButton")
self.gridLayout_6.addWidget(self.checkableButton, 1, 0, 1, 1)
self.pushButton_5 = QtWidgets.QPushButton(self.tab_3)
self.pushButton_5.setObjectName("pushButton_5")
self.gridLayout_6.addWidget(self.pushButton_5, 2, 0, 1, 1)
self.pushButton_4 = QtWidgets.QPushButton(self.tab_3)
self.pushButton_4.setMinimumSize(QtCore.QSize(132, 50))
self.pushButton_4.setObjectName("pushButton_4")
self.gridLayout_6.addWidget(self.pushButton_4, 3, 0, 1, 1)
self.tabWidget_2.addTab(self.tab_3, "")
self.gridLayout.addWidget(self.tabWidget_2, 1, 0, 1, 1)
self.tabWidget.addTab(self.tab, "")
self.verticalLayout_5.addWidget(self.tabWidget)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 1055, 21))
self.menubar.setObjectName("menubar")
self.menuMenu = QtWidgets.QMenu(self.menubar)
self.menuMenu.setObjectName("menuMenu")
self.menuSubmenu_2 = QtWidgets.QMenu(self.menuMenu)
self.menuSubmenu_2.setObjectName("menuSubmenu_2")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.toolBar = QtWidgets.QToolBar(MainWindow)
self.toolBar.setObjectName("toolBar")
MainWindow.addToolBar(QtCore.Qt.TopToolBarArea, self.toolBar)
self.dockWidget1 = QtWidgets.QDockWidget(MainWindow)
self.dockWidget1.setMinimumSize(QtCore.QSize(150, 284))
self.dockWidget1.setObjectName("dockWidget1")
self.dockWidgetContents = QtWidgets.QWidget()
self.dockWidgetContents.setObjectName("dockWidgetContents")
self.verticalLayout_10 = QtWidgets.QVBoxLayout(self.dockWidgetContents)
self.verticalLayout_10.setObjectName("verticalLayout_10")
self.toolBox = QtWidgets.QToolBox(self.dockWidgetContents)
self.toolBox.setLayoutDirection(QtCore.Qt.LeftToRight)
self.toolBox.setObjectName("toolBox")
self.page = QtWidgets.QWidget()
self.page.setGeometry(QtCore.QRect(0, 0, 132, 211))
self.page.setObjectName("page")
self.gridLayout_2 = QtWidgets.QGridLayout(self.page)
self.gridLayout_2.setObjectName("gridLayout_2")
self.listWidget = QtWidgets.QListWidget(self.page)
self.listWidget.setObjectName("listWidget")
self.listWidget.setDragEnabled(True)
item = QtWidgets.QListWidgetItem()
self.listWidget.addItem(item)
item = QtWidgets.QListWidgetItem()
self.listWidget.addItem(item)
item = QtWidgets.QListWidgetItem()
self.listWidget.addItem(item)
item = QtWidgets.QListWidgetItem()
self.listWidget.addItem(item)
item = QtWidgets.QListWidgetItem()
self.listWidget.addItem(item)
item = QtWidgets.QListWidgetItem()
self.listWidget.addItem(item)
item = QtWidgets.QListWidgetItem()
self.listWidget.addItem(item)
self.gridLayout_2.addWidget(self.listWidget, 0, 0, 1, 1)
self.toolBox.addItem(self.page, "")
self.page_2 = QtWidgets.QWidget()
self.page_2.setGeometry(QtCore.QRect(0, 0, 132, 211))
self.page_2.setObjectName("page_2")
self.verticalLayout_2 = QtWidgets.QVBoxLayout(self.page_2)
self.verticalLayout_2.setObjectName("verticalLayout_2")
self.listWidget_2 = QtWidgets.QListWidget(self.page_2)
self.listWidget_2.setObjectName("listWidget_2")
item = QtWidgets.QListWidgetItem()
self.listWidget_2.addItem(item)
item = QtWidgets.QListWidgetItem()
self.listWidget_2.addItem(item)
item = QtWidgets.QListWidgetItem()
self.listWidget_2.addItem(item)
item = QtWidgets.QListWidgetItem()
self.listWidget_2.addItem(item)
self.verticalLayout_2.addWidget(self.listWidget_2)
self.verticalWidget = QtWidgets.QWidget(self.page_2)
self.verticalWidget.setObjectName("verticalWidget")
self.verticalLayout_3 = QtWidgets.QVBoxLayout(self.verticalWidget)
self.verticalLayout_3.setObjectName("verticalLayout_3")
self.verticalLayout_2.addWidget(self.verticalWidget, 0, QtCore.Qt.AlignTop)
self.toolBox.addItem(self.page_2, "")
self.page_7 = QtWidgets.QWidget()
self.page_7.setGeometry(QtCore.QRect(0, 0, 132, 211))
self.page_7.setObjectName("page_7")
self.verticalLayout_4 = QtWidgets.QVBoxLayout(self.page_7)
self.verticalLayout_4.setObjectName("verticalLayout_4")
self.verticalWidget1 = QtWidgets.QWidget(self.page_7)
self.verticalWidget1.setObjectName("verticalWidget1")
self.verticalLayout_7 = QtWidgets.QVBoxLayout(self.verticalWidget1)
self.verticalLayout_7.setObjectName("verticalLayout_7")
self.listWidget_3 = QtWidgets.QListWidget(self.verticalWidget1)
self.listWidget_3.setObjectName("listWidget_3")
item = QtWidgets.QListWidgetItem()
self.listWidget_3.addItem(item)
item = QtWidgets.QListWidgetItem()
self.listWidget_3.addItem(item)
item = QtWidgets.QListWidgetItem()
self.listWidget_3.addItem(item)
item = QtWidgets.QListWidgetItem()
self.listWidget_3.addItem(item)
item = QtWidgets.QListWidgetItem()
self.listWidget_3.addItem(item)
self.verticalLayout_7.addWidget(self.listWidget_3)
self.verticalLayout_4.addWidget(self.verticalWidget1, 0, QtCore.Qt.AlignTop)
self.toolBox.addItem(self.page_7, "")
self.verticalLayout_10.addWidget(self.toolBox)
self.dockWidget1.setWidget(self.dockWidgetContents)
MainWindow.addDockWidget(QtCore.Qt.DockWidgetArea(1), self.dockWidget1)
self.dockWidget2 = QtWidgets.QDockWidget(MainWindow)
self.dockWidget2.setObjectName("dockWidget2")
self.dockWidgetContents_2 = QtWidgets.QWidget()
self.dockWidgetContents_2.setObjectName("dockWidgetContents_2")
self.gridLayout_3 = QtWidgets.QGridLayout(self.dockWidgetContents_2)
self.gridLayout_3.setObjectName("gridLayout_3")
self.toolBox_2 = QtWidgets.QToolBox(self.dockWidgetContents_2)
self.toolBox_2.setObjectName("toolBox_2")
self.page_5 = QtWidgets.QWidget()
self.page_5.setGeometry(QtCore.QRect(0, 0, 132, 347))
self.page_5.setObjectName("page_5")
self.verticalLayout_8 = QtWidgets.QVBoxLayout(self.page_5)
self.verticalLayout_8.setObjectName("verticalLayout_8")
self.verticalWidget2 = QtWidgets.QWidget(self.page_5)
self.verticalWidget2.setObjectName("verticalWidget2")
self.verticalLayout_9 = QtWidgets.QVBoxLayout(self.verticalWidget2)
self.verticalLayout_9.setObjectName("verticalLayout_9")
self.pushButton_24 = QtWidgets.QPushButton(self.verticalWidget2)
self.pushButton_24.setObjectName("pushButton_24")
self.verticalLayout_9.addWidget(self.pushButton_24)
self.pushButton_25 = QtWidgets.QPushButton(self.verticalWidget2)
self.pushButton_25.setObjectName("pushButton_25")
self.verticalLayout_9.addWidget(self.pushButton_25)
self.pushButton_29 = QtWidgets.QPushButton(self.verticalWidget2)
self.pushButton_29.setObjectName("pushButton_29")
self.verticalLayout_9.addWidget(self.pushButton_29)
self.pushButton_30 = QtWidgets.QPushButton(self.verticalWidget2)
self.pushButton_30.setObjectName("pushButton_30")
self.verticalLayout_9.addWidget(self.pushButton_30)
self.pushButton_28 = QtWidgets.QPushButton(self.verticalWidget2)
self.pushButton_28.setObjectName("pushButton_28")
self.verticalLayout_9.addWidget(self.pushButton_28)
self.pushButton_27 = QtWidgets.QPushButton(self.verticalWidget2)
self.pushButton_27.setObjectName("pushButton_27")
self.verticalLayout_9.addWidget(self.pushButton_27)
self.pushButton_26 = QtWidgets.QPushButton(self.verticalWidget2)
self.pushButton_26.setObjectName("pushButton_26")
self.verticalLayout_9.addWidget(self.pushButton_26)
self.pushButton_23 = QtWidgets.QPushButton(self.verticalWidget2)
self.pushButton_23.setObjectName("pushButton_23")
self.verticalLayout_9.addWidget(self.pushButton_23)
self.verticalLayout_8.addWidget(self.verticalWidget2, 0, QtCore.Qt.AlignTop)
self.toolBox_2.addItem(self.page_5, "")
self.gridLayout_3.addWidget(self.toolBox_2, 0, 0, 1, 1)
self.dockWidget2.setWidget(self.dockWidgetContents_2)
MainWindow.addDockWidget(QtCore.Qt.DockWidgetArea(1), self.dockWidget2)
self.toolBar_2 = QtWidgets.QToolBar(MainWindow)
self.toolBar_2.setObjectName("toolBar_2")
MainWindow.addToolBar(QtCore.Qt.TopToolBarArea, self.toolBar_2)
self.actionAction = QtWidgets.QAction(MainWindow)
self.actionAction.setObjectName("actionAction")
self.actionSub_menu = QtWidgets.QAction(MainWindow)
self.actionSub_menu.setObjectName("actionSub_menu")
self.actionAction_C = QtWidgets.QAction(MainWindow)
self.actionAction_C.setObjectName("actionAction_C")
self.dockWidget1.raise_()
self.menuSubmenu_2.addAction(self.actionSub_menu)
self.menuSubmenu_2.addAction(self.actionAction_C)
self.menuMenu.addAction(self.actionAction)
self.menuMenu.addAction(self.menuSubmenu_2.menuAction())
self.menubar.addAction(self.menuMenu.menuAction())
self.toolBar.addAction(self.actionAction)
self.toolBar.addSeparator()
self.toolBar.addAction(self.actionSub_menu)
self.retranslateUi(MainWindow)
self.tabWidget.setCurrentIndex(0)
self.tabWidget_2.setCurrentIndex(0)
self.toolBox.setCurrentIndex(2)
self.toolBox_2.setCurrentIndex(0)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
MainWindow.setTabOrder(self.pushButton, self.checkableButton)
MainWindow.setTabOrder(self.checkableButton, self.pushButton_5)
MainWindow.setTabOrder(self.pushButton_5, self.tabWidget)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.plainTextEdit_3.setPlainText(_translate("MainWindow", "#include <stdlib.h>\n"
"\n"
"main(){\n"
" printf ( Hello Wolrd!\");\n"
" return 0;\n"
"}"))
self.pushButton.setText(_translate("MainWindow", "Compile"))
self.checkableButton.setText(_translate("MainWindow", "Compile and Run"))
self.pushButton_5.setText(_translate("MainWindow", "Run"))
self.pushButton_4.setText(_translate("MainWindow", "Değişken Yarat"))
self.tabWidget_2.setTabText(self.tabWidget_2.indexOf(self.tab_3), _translate("MainWindow", "Tab 1"))
self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab), _translate("MainWindow", "Tab 1"))
self.menuMenu.setTitle(_translate("MainWindow", "&Menu"))
self.menuSubmenu_2.setTitle(_translate("MainWindow", "&Submenu 2"))
self.toolBar.setWindowTitle(_translate("MainWindow", "toolBar"))
self.dockWidget1.setWindowTitle(_translate("MainWindow", "Yapılar"))
__sortingEnabled = self.listWidget.isSortingEnabled()
self.listWidget.setSortingEnabled(False)
item = self.listWidget.item(0)
item.setText(_translate("MainWindow", "Topla"))
item = self.listWidget.item(1)
item.setText(_translate("MainWindow", "Çıkar"))
item = self.listWidget.item(2)
item.setText(_translate("MainWindow", "Çarp"))
item = self.listWidget.item(3)
item.setText(_translate("MainWindow", "Böl"))
item = self.listWidget.item(4)
item.setText(_translate("MainWindow", "Üst Al"))
item = self.listWidget.item(5)
item.setText(_translate("MainWindow", "Kök Al"))
item = self.listWidget.item(6)
item.setText(_translate("MainWindow", "Mod Al"))
self.listWidget.setSortingEnabled(__sortingEnabled)
self.toolBox.setItemText(self.toolBox.indexOf(self.page), _translate("MainWindow", "İşlemler"))
__sortingEnabled = self.listWidget_2.isSortingEnabled()
self.listWidget_2.setSortingEnabled(False)
item = self.listWidget_2.item(0)
item.setText(_translate("MainWindow", "For"))
item = self.listWidget_2.item(1)
item.setText(_translate("MainWindow", "While"))
item = self.listWidget_2.item(2)
item.setText(_translate("MainWindow", "If"))
item = self.listWidget_2.item(3)
item.setText(_translate("MainWindow", "Switch / Case"))
self.listWidget_2.setSortingEnabled(__sortingEnabled)
self.toolBox.setItemText(self.toolBox.indexOf(self.page_2), _translate("MainWindow", "Döngüler"))
__sortingEnabled = self.listWidget_3.isSortingEnabled()
self.listWidget_3.setSortingEnabled(False)
item = self.listWidget_3.item(0)
item.setText(_translate("MainWindow", "Değer Al"))
item = self.listWidget_3.item(1)
item.setText(_translate("MainWindow", "Atama Yap"))
item = self.listWidget_3.item(2)
item.setText(_translate("MainWindow", "Çıktı Yaz"))
item = self.listWidget_3.item(3)
item.setText(_translate("MainWindow", "Kes"))
item = self.listWidget_3.item(4)
item.setText(_translate("MainWindow", "Devam Et"))
self.listWidget_3.setSortingEnabled(__sortingEnabled)
self.toolBox.setItemText(self.toolBox.indexOf(self.page_7), _translate("MainWindow", "Deyimler"))
self.dockWidget2.setWindowTitle(_translate("MainWindow", "Algoritmalar"))
self.pushButton_24.setText(_translate("MainWindow", "Quick Sort"))
self.pushButton_25.setText(_translate("MainWindow", "Merge Sort"))
self.pushButton_29.setText(_translate("MainWindow", "Radix Sort"))
self.pushButton_30.setText(_translate("MainWindow", "Insertion Sort"))
self.pushButton_28.setText(_translate("MainWindow", "Bubble Sort"))
self.pushButton_27.setText(_translate("MainWindow", "Counting Sort"))
self.pushButton_26.setText(_translate("MainWindow", "Heap Sort"))
self.pushButton_23.setText(_translate("MainWindow", "Selection Sort"))
self.toolBox_2.setItemText(self.toolBox_2.indexOf(self.page_5), _translate("MainWindow", "Sıralama Algoritması"))
self.toolBar_2.setWindowTitle(_translate("MainWindow", "toolBar_2"))
self.actionAction.setText(_translate("MainWindow", "&Action"))
self.actionSub_menu.setText(_translate("MainWindow", "&Action B"))
self.actionSub_menu.setToolTip(_translate("MainWindow", "submenu"))
self.actionAction_C.setText(_translate("MainWindow", "Action &C"))
import style_Dark_rc
import style_black_rc
import sys
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
ex = Ui_MainWindow()
w = QtWidgets.QMainWindow()
ex.setupUi(w)
w.show()
sys.exit(app.exec_())
解释:
QPlainTextEdit 仅支持纯文本,例如拖放信息,但在 QListWidget 的情况下,它使用另一个 mimeType 发送信息,因此失败。
解法:
有两种选择:向QPlaintTextedit 添加接受另一个mimeType 数据的功能,或者让信息源(在本例中为QListWidget)以纯文本形式发送信息。在这种情况下,我将实施后者,因为它是最简单的解决方案。
当信息通过拖放从一个元素发送到另一个元素时,使用 QMimeData,它通过源发送的事件和接收者接收的事件发送,在 QListWidget 的情况下它有将项目转换为 QMimeData 的方法 mimeData()
,以便可以通过添加纯文本来覆盖该方法。那么我们只在脚本中导入:
# ...
class ListWidget(QtWidgets.QListWidget):
def mimeData(self, items):
md = super().mimeData(items)
text = "".join([it.text() for it in items])
md.setText(text)
return md
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
# ...
self.gridLayout_2.setObjectName("gridLayout_2")
# self.listWidget = QtWidgets.QListWidget(self.page)
<b>self.listWidget = ListWidget(self.page)</b>
self.listWidget.setObjectName("listWidget")
# ...