I need open other window (configuration windows) in my app.
Each window is a different class. I would like to know if I can group everything in the same class and open the window through a function.
from PyQt5 import QtWidgets, uic
import sys
class OpenMyApp(QtWidgets.QMainWindow):
# My App
pathUi = 'ui/interface.ui'
pathStylesheet = 'ui/stylesheet.qss'
def __init__(self):
super(OpenMyApp, self).__init__()
self.init_interface()
def init_interface(self):
self.ui = uic.loadUi(self.pathUi, self)
style_app = self.pathStylesheet
with open(style_app, "r") as fh:
self.setStyleSheet(fh.read())
self.ui.btn_openConfigApp.clicked.connect(self.open_config)
def open_config(self):
pass
class OpenMyConfigApp(QtWidgets.QMainWindow):
# My Config App
pathUi = 'ui/config-interface.ui'
pathStylesheet = 'ui/stylesheet.qss'
def __init__(self):
super(OpenMyConfigApp, self).__init__()
self.init_interface()
def init_interface(self):
self.ui = uic.loadUi(self.pathUi, self)
style_app = self.pathStylesheet
with open(style_app, "r") as fh:
self.setStyleSheet(fh.read())
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
window = OpenMyApp()
window.show()
sys.exit(app.exec_())
Related
I have opened the file from the current directory in "opened" method, here file is opening and reading the data in it, but I want to write the read data in MDI subwindow.
from PyQt5 import QtWidgets
from PyQt5 import QtGui
from PyQt5 import QtCore
from PyQt5.QtWidgets import QMdiArea, QMdiSubWindow
from PyQt5.Qt import QMenu
import os
import tree2
class fileview(tree2.Ui_FOSApplication,QtWidgets.QMainWindow):
count = 0
def __init__(self):
super(fileview, self).__init__()
self.setupUi(self)
self.treeView.setContextMenuPolicy(QtCore.Qt.CustomContextMenu)
self.treeView.customContextMenuRequested.connect(self.context_view)
self.populate()
def populate(self):
path = 'C:\FOS Application'
self.model = QtWidgets.QFileSystemModel()
self.model.setRootPath(QtCore.QDir.rootPath())
self.treeView.setModel(self.model)
self.treeView.setRootIndex(self.model.index(path))
#self.treeView.doubleClicked.connect(self.context_view)
#self.treeView.setSortingEnabled(True)
self.treeView.setColumnHidden(1, True)
self.treeView.setColumnHidden(2, True)
self.treeView.setColumnHidden(3, True)
def context_view(self):
view = QtWidgets.QMenu()
open = view.addAction("Open")
open.triggered.connect(self.subwindow)
curser = QtGui.QCursor()
view.exec_(curser.pos())
def subwindow(self):
fileview.count = fileview.count+1
self.w = self.openfile()
self.sub = QMdiSubWindow()
self.sub.setWidget(self.w)
#self.mdiArea.activeSubWindow(self.sub)
self.mdiArea.addSubWindow(self.sub)
self.sub.show()
def openfile(self):
index = self.treeView.currentIndex()
file_name = self.model.filePath(index)
os.startfile(file_name)
if __name__ == '__main__':
app = QtWidgets.QApplication([])`enter code here`
fb = fileview()
fb.show()
app.exec_()
How can I add a context menu to the items in my QToolBar. The reason for the context menu, is i want to give each Action a right-click > Delete action. Similar to the Chrome browsers ability to remove bookmarks from the bookmark toolbar. How do i achieve this in Pyside?
import os, sys
from PySide import QtGui, QtCore
class Example(QtGui.QMainWindow):
def __init__(self):
super(Example, self).__init__()
self.resize(300,200)
self.createToolBars()
self.createActions()
def createToolBars(self):
self.toolBar = self.addToolBar('Presets')
self.toolBar.setIconSize(QtCore.QSize(16,16))
self.toolBar.setToolButtonStyle(QtCore.Qt.ToolButtonTextUnderIcon)
def createActions(self):
self.toolBar.clear()
presets = ['Abby','Kevin','Bob','Mary']
for x in presets:
act = QtGui.QAction(x, self)
act.setData(x)
self.toolBar.addAction(act)
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
ex = Example()
ex.show()
sys.exit(app.exec_())
You have to implement a custom QToolBar and implement the contextMenuEvent method:
import os
import sys
from PySide import QtCore, QtGui
class ToolBar(QtGui.QToolBar):
def contextMenuEvent(self, event):
current_action = self.actionAt(event.pos())
if current_action is None:
return
menu = QtGui.QMenu()
delete_action = menu.addAction("delete")
action = menu.exec_(event.globalPos())
if action == delete_action:
self.removeAction(current_action)
class Example(QtGui.QMainWindow):
def __init__(self, parent=None):
super(Example, self).__init__(parent)
self.resize(300,200)
self.createToolBars()
self.createActions()
def createToolBars(self):
self.tool_bar = ToolBar('Presets', self)
self.addToolBar(self.tool_bar)
self.tool_bar.setIconSize(QtCore.QSize(16,16))
self.tool_bar.setToolButtonStyle(QtCore.Qt.ToolButtonTextUnderIcon)
def createActions(self):
self.tool_bar.clear()
presets = ['Abby','Kevin','Bob','Mary']
for x in presets:
act = QtGui.QAction(x, self)
act.setData(x)
self.tool_bar.addAction(act)
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
ex = Example()
ex.show()
sys.exit(app.exec_())
I am trying to copy a file with a progress bar included in PyQt5. I can see in a console with print("Copied: {}".format(percent)) that 100% is copied.
However, my progress bar does not update my copy process. How can I check that the signal was sent? Where did I make mistake?
import os, sys
from PyQt5 import QtWidgets, QtCore
class Extended(QtCore.QThread):
def __init__(self):
super().__init__()
self.src_file = 'test.txt'
self.dst_file = 'test_copy.txt'
self.copyfileobj(self.src_file, self.dst_file, self.cb)
copied_percent = QtCore.pyqtSignal(int)
def cb(self, temp_file_size):
self.file_size = os.stat(self.src_file).st_size
percent = int(temp_file_size/self.file_size*100)
print("Copied: {}".format(percent))
self.copied_percent.emit(percent)
def copyfileobj(self, fsrc, fdst, callback, length=16*1024):
copied = 0
with open(fsrc, "rb") as fr, open(fdst, "wb") as fw:
while True:
buff = fr.read(length)
if not buff:
break
fw.write(buff)
copied += len(buff)
callback(copied)
class MyApp(QtWidgets.QWidget):
def __init__(self, parent=None):
super().__init__(parent)
self.w = QtWidgets.QWidget()
self.w.setWindowTitle("Progress bar copy test")
# Add widgets on the window
self.copy_button = QtWidgets.QPushButton("Copy", self)
self.copy_button.sizeHint()
self.progress_bar = QtWidgets.QProgressBar(self)
self.progress_bar.setGeometry(0, 40, 300, 25)
self.progress_bar.setMaximum(100)
self.copy_button.clicked.connect(self.on_button_click)
def on_button_click(self):
self.copy_button.setDisabled(True)
self.ext = Extended()
self.ext.copied_percent.connect(self.on_count_change)
self.ext.start()
def on_count_change(self, value):
self.progress_bar.setValue(value)
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
prog = MyApp()
prog.show()
sys.exit(app.exec_())
Ok, I figured it out. There has to be a run method in a thread-supported class which is executed when you call a start() method on a instance object self.ext. I also added self.ext.join() method to not end a program before the thread is executed and finished.
import os, sys
from PyQt5 import QtWidgets, QtCore
class Extended(QtCore.QThread):
"""
For running copy operation
"""
copied_percent_signal= QtCore.pyqtSignal(int)
def __init__(self):
super().__init__()
self.src_file = 'test.txt'
self.dst_file = 'test_copy.txt'
self.file_size = os.stat(self.src_file).st_size
def run(self):
self.copyfileobj(self.src_file, self.dst_file, self.my_callback)
def my_callback(self, temp_file_size):
percent = int(temp_file_size/self.file_size*100)
print("Copiedd: {}".format(percent))
self.copied_percent_signal.emit(percent)
def copyfileobj(self, fsrc, fdst, callback, length=16*1024):
copied = 0
with open(fsrc, "rb") as fr, open(fdst, "wb") as fw:
while True:
buff = fr.read(length)
if not buff:
break
fw.write(buff)
copied += len(buff)
callback(copied)
class MyApp(QtWidgets.QWidget):
def __init__(self, parent=None):
super().__init__(parent)
# Instance attribute defined later in on_button_click()
self.ext = None
self.w = QtWidgets.QWidget()
self.w.setWindowTitle("Progress bar copy test")
# Add widgets on the window
self.copy_button = QtWidgets.QPushButton("Copy", self)
self.copy_button.sizeHint()
self.progress_bar = QtWidgets.QProgressBar(self)
self.progress_bar.setGeometry(0, 40, 300, 25)
self.progress_bar.setMaximum(100)
self.copy_button.clicked.connect(self.on_button_click)
def on_button_click(self):
self.copy_button.setDisabled(True)
self.ext = Extended()
self.ext.copied_percent_signal.connect(self.on_count_change)
self.ext.start()
self.ext.join()
def on_count_change(self, value):
self.progress_bar.setValue(value)
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
prog = MyApp()
prog.show()
sys.exit(app.exec_())
My file is being copied and the progress bar is working.
I've recently started learning PyQt on my own and I've come in some trouble trying to write a custom class that inherits from QPushButton so I can adjust its attributes. I'm trying to pass a text as an argument whenever I initialize an object of this class. I am pretty sure there's something wrong with my init but I haven't found it yet.
Here is the code:
import sys
from PySide import QtGui, QtCore
class mainb(QtGui.QPushButton):
def __init__(Text,self, parent = None):
super().__init__(parent)
self.setupbt(Text)
def setupbt(self):
self.setFlat(True)
self.setText(Text)
self.setGeometry(200,100, 60, 35)
self.move(300,300)
print('chegu aqui')
self.setToolTip('Isso é muito maneiro <b>Artur</b>')
self.show()
class mainwindow(QtGui.QWidget):
def __init__(self , parent = None):
super().__init__()
self.setupgui()
def setupgui(self):
self.setToolTip('Oi <i>QWidget</i> widget')
self.resize(800,600)
self.setWindowTitle('Janela do Artur')
af = mainb("Bom dia",self)
self.show()
"""
btn = QtGui.QPushButton('Botão',self)
btn.clicked.connect(QtCore.QCoreApplication.instance().quit)
btn.resize(btn.sizeHint())
btn.move(300, 50)
"""
def main():
app = QtGui.QApplication(sys.argv)
ex = mainwindow()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
You are using super in wrong way, super must get a instance and another thing your first arg is Text, that's wrong that should be self. I fixed some more and the below code should work for you
import sys
from PySide import QtGui, QtCore
class mainb(QtGui.QPushButton):
def __init__(self, Text, parent = None):
super(mainb, self).__init__()
self.setupbt(Text)
def setupbt(self, Text):
self.setFlat(True)
self.setText(Text)
self.setGeometry(200,100, 60, 35)
self.move(300,300)
print('chegu aqui')
self.setToolTip('Isso muito maneiro <b>Artur</b>')
self.show()
class mainwindow(QtGui.QWidget):
def __init__(self , parent = None):
super(mainwindow, self).__init__()
self.setupgui()
def setupgui(self):
self.setToolTip('Oi <i>QWidget</i> widget')
self.resize(800,600)
self.setWindowTitle('Janela do Artur')
newLayout = QtGui.QHBoxLayout()
af = mainb("Bom dia",self)
newLayout.addWidget(af)
self.setLayout(newLayout)
self.show()
def main():
app = QtGui.QApplication(sys.argv)
ex = mainwindow()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
Your def setupbt(self) does not seem to take the text as argument. Try def setupbt(self, Text): instead.
I'm using Python 2.7 and Qt designer and I'm new to MVC:
I have a View completed within Qt to give me a directory tree list, and the controller in place to run things. My question is:
Given a Qtree view, how may I obtain a directory once a dir is selected?
Code snap shot is below, I suspect it's SIGNAL(..) though I'm unsure:
class Main(QtGui.QMainWindow):
plot = pyqtSignal()
def __init__(self):
QtGui.QMainWindow.__init__(self)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
# create model
model = QtGui.QFileSystemModel()
model.setRootPath( QtCore.QDir.currentPath() )
# set the model
self.ui.treeView.setModel(model)
**QtCore.QObject.connect(self.ui.treeView, QtCore.SIGNAL('clicked()'), self.test)**
def test(self):
print "hello!"
The signal you're looking for is selectionChanged emmited by the selectionModel owned by your tree. This signal is emmited with the selected item as first argument and the deselected as second, both are instances of QItemSelection.
So you might want to change the line:
QtCore.QObject.connect(self.ui.treeView, QtCore.SIGNAL('clicked()'), self.test)
to
QtCore.QObject.connect(self.ui.treeView.selectionModel(), QtCore.SIGNAL('selectionChanged()'), self.test)
Also I recommend you to use the new style for signals and slots. Redefine your test function as:
#QtCore.pyqtSlot("QItemSelection, QItemSelection")
def test(self, selected, deselected):
print("hello!")
print(selected)
print(deselected)
Here you have a working example:
from PyQt4 import QtGui
from PyQt4 import QtCore
class Main(QtGui.QTreeView):
def __init__(self):
QtGui.QTreeView.__init__(self)
model = QtGui.QFileSystemModel()
model.setRootPath( QtCore.QDir.currentPath() )
self.setModel(model)
QtCore.QObject.connect(self.selectionModel(), QtCore.SIGNAL('selectionChanged(QItemSelection, QItemSelection)'), self.test)
#QtCore.pyqtSlot("QItemSelection, QItemSelection")
def test(self, selected, deselected):
print("hello!")
print(selected)
print(deselected)
if __name__ == '__main__':
import sys
app = QtGui.QApplication(sys.argv)
w = Main()
w.show()
sys.exit(app.exec_())
PyQt5
In PyQt5 is a little bit different (thanks to Carel and saldenisov for comments and aswer.)
... connect moved from being an object method to a method acting upon the attribute when PyQt went from 4 to 5
So instead the known:
QtCore.QObject.connect(self.ui.treeView, QtCore.SIGNAL('clicked()'), self.test)
now you write:
class Main(QTreeView):
def __init__(self):
# ...
self.setModel(model)
self.doubleClicked.connect(self.test) # Note that the the signal is now a attribute of the widget.
Here is a the example (by saldenisov) using PyQt5.
from PyQt5.QtWidgets import QTreeView,QFileSystemModel,QApplication
class Main(QTreeView):
def __init__(self):
QTreeView.__init__(self)
model = QFileSystemModel()
model.setRootPath('C:\\')
self.setModel(model)
self.doubleClicked.connect(self.test)
def test(self, signal):
file_path=self.model().filePath(signal)
print(file_path)
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
w = Main()
w.show()
sys.exit(app.exec_())
In PyQt5 it can be done in this way:
from PyQt5.QtWidgets import QTreeView,QFileSystemModel,QApplication
class Main(QTreeView):
def __init__(self):
QTreeView.__init__(self)
model = QFileSystemModel()
model.setRootPath('C:\\')
self.setModel(model)
self.doubleClicked.connect(self.test)
def test(self, signal):
file_path=self.model().filePath(signal)
print(file_path)
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
w = Main()
w.show()
sys.exit(app.exec_())
If I understand the question correctly you would like the directory or file name selected.
This is what I do:
from PyQt4 import QtGui
from PyQt4 import QtCore
# ---------------------------------------------------------------------
class MainWindow(QtGui.QMainWindow):
def __init__(self, parent=None):
super().__init__(parent)
self.resize(600,400)
self.setWindowTitle("Treeview Example")
self.treeview = QtGui.QTreeView(self)
self.treeview.model = QtGui.QFileSystemModel()
self.treeview.model.setRootPath( QtCore.QDir.currentPath() )
self.treeview.setModel(self.treeview.model)
self.treeview.setColumnWidth(0, 200)
self.setCentralWidget(self.treeview)
self.treeview.clicked.connect(self.on_treeview_clicked)
# ---------------------------------------------------------------------
#QtCore.pyqtSlot(QtCore.QModelIndex)
def on_treeview_clicked(self, index):
indexItem = self.treeview.model.index(index.row(), 0, index.parent())
# path or filename selected
fileName = self.treeview.model.fileName(indexItem)
# full path/filename selected
filePath = self.treeview.model.filePath(indexItem)
print(fileName)
print(filePath)
# ---------------------------------------------------------------------
if __name__ == '__main__':
import sys
app = QtGui.QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())
I tried this alternative to get the file name...
Instead of:
indexItem = self.treeview.model.index(index.row(), 0, index.parent())
# path or filename selected
fileName = self.treeview.model.fileName(indexItem)
I tried:
# path or filename selected
fileName = index.internalPointer().fileName
Which also seems to work...