A Gui(only a edit box and a button) is created in Qt Designer, when clicking the button, a window shows, and the selected file'path appears in the edit box. But when I clicked the button, two windows are opened, the slot is coded as follow:
def on_openBtn_clicked(self):
fileName = QFileDialog.getOpenFileName(
self,'choose the file',options = QFileDialog.DontUseNativeDialog)
self.fileRoadEdt.setText(fileName)
then I edit the code like:
def on_openBtn_clicked(self):
dialog = QFileDialog(self)
dialog.exec_()
for fileName in dialog.selectedFiles():
self.fileRoadEdt.setText(fileName)
the same thing happen, what mistake am I making?
Related
OS: W10. This may be significant. If you have different results on a different platform, feedback would be helpful.
Here is an MRE. If you run it and go Ctrl+O, the menu labels become greyed. If you select a file in the QFileDialog by clicking the "Open" button or using its mnemonic (Alt+O), the open-file dialog is dismissed and the "Files" and "Help" menus become un-greyed.
However, if you go Ctrl+O again, and this time enter the name of a file in the "File name" box (QLineEdit), and then press Return, the dialog is dismissed (with a successful selection result) but the "Files" and "Help" menus remain greyed-out. It looks like this:
import sys, os
from PyQt5 import QtWidgets, QtCore, QtGui
class MainWindow(QtWidgets.QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle('Greying of menus MRE')
self.setGeometry(QtCore.QRect(100, 100, 400, 200))
menubar = QtWidgets.QMenuBar(self)
self.setMenuBar(menubar)
self.files_menu = QtWidgets.QMenu('&Files', self)
menubar.addMenu(self.files_menu)
self.help_menu = QtWidgets.QMenu('&Help', self)
menubar.addMenu(self.help_menu)
self.new_action = QtWidgets.QAction('&New', self)
self.files_menu.addAction(self.new_action)
self.open_action = QtWidgets.QAction('&Open', self)
self.files_menu.addAction(self.open_action)
self.open_action.setShortcut("Ctrl+O")
self.open_action.triggered.connect(self.open_file)
def focusInEvent(self, event ):
print('main_window focusInEvent')
super().focusInEvent(event)
def focusOutEvent(self, event ):
print('main_window focusOutEvent')
super().focusInEvent(event)
def activateWindow(self):
print('main_window activateWindow')
super().activateWindow()
def open_file(self):
print('open file')
main_window_self = self
# open_doc_dialog = QtWidgets.QFileDialog(self.get_main_window())
class OpenDocFileDialog(QtWidgets.QFileDialog):
def accepted(self):
print('accepted')
super().accepted()
def accept(self):
print('accept')
super().accept()
def close(self):
print('close')
super().close()
def done(self, r):
print(f'done r {r}')
# neither of these solves the problem:
# main_window_self.activateWindow()
# main_window_self.files_menu.activateWindow()
super().done(r)
def hide(self):
print(f'hide')
super().hide()
def focusInEvent(self, event ):
print('focusInEvent')
super().focusInEvent(event)
def focusOutEvent(self, event ):
print('focusOutEvent')
super().focusInEvent(event)
def activateWindow(self):
print('activateWindow')
super().activateWindow()
open_doc_dialog = OpenDocFileDialog(self)
open_doc_dialog.setWindowTitle('Choose file')
open_doc_dialog.setDirectory(os.getcwd())
# we cannot use the native dialog, because we need control over the UI
options = open_doc_dialog.Options(open_doc_dialog.DontUseNativeDialog)
open_doc_dialog.setOptions(options)
open_doc_button = open_doc_dialog.findChild(QtWidgets.QDialogButtonBox).button(QtWidgets.QDialogButtonBox.Open)
lineEdit = open_doc_dialog.findChild(QtWidgets.QLineEdit)
# this does not solve the problem
# lineEdit.returnPressed.disconnect()
# lineEdit.returnPressed.connect(open_doc_button.click)
print(f'open_doc_button {open_doc_button}, lineEdit {lineEdit}')
# show the dialog
dialog_code = open_doc_dialog.exec()
if dialog_code != QtWidgets.QDialog.Accepted: return
sel_files = open_doc_dialog.selectedFiles()
print(f'sel_files: {sel_files}')
app = QtWidgets.QApplication([])
main_window = MainWindow()
main_window.show()
sys.exit(app.exec())
This problem can be understood, if not solved, with reference to this answer.
Note that this greying-out is not disablement. As explained in the above link, this has to do with "active/inactive states" of the menus (or their labels). The menus remain enabled throughout, although in this case it's impossible to know that while the open-file dialog is showing because it is modal. Clicking on one menu after the dialog has gone, or just hovering over it, is enough to un-grey them both...
The explanation, as I understand it, is that the "File name" box QLineEdit has a signal, returnPressed, which appears to activate something subtley different to the slot which is invoked when you use the "Choose" button. You can see I have experimented with trying to re-wire that signal, to no avail.
The method done of the QFileDialog appears to be called however the dialog closes (unlike close!), so I tried "activating" the main window... and then the individual QMenus... Doesn't work.
I am not clear how to get a handle on this "active state" business or why the slot connected to returnPressed is (seemingly) unable to give the "active state" back to the menus when the other slot manages to do so.
Edit
Searching on Musicamante's "unpolishing" suggestion led me to this:
lineEdit.returnPressed.disconnect()
def return_pressed():
style = main_window_self.menubar.style()
style.unpolish(main_window_self.menubar)
open_doc_button.click()
lineEdit.returnPressed.connect(return_pressed)
... unfortunately this doesn't work.
This looks like a possible Windows-related bug, since I can't reproduce it on Linux. As a work-around, you could try forcing a repaint after the dialog closes:
# show the dialog
dialog_code = open_doc_dialog.exec()
self.menubar.repaint()
Finally got it, thanks to Musicamante's suggestion:
lineEdit.returnPressed.disconnect()
def return_pressed():
style = main_window_self.menubar.style()
style.unpolish(main_window_self.menubar)
open_doc_button.click()
main_window_self.menubar.repaint()
lineEdit.returnPressed.connect(return_pressed)
... I actually tried this several times, just to make sure it was doing what was intended. So in fact, fortunately, no single-shot timer was needed in this case.
when I opened my Top Level window, I always saw an annoying and weird behaviour.. at the end I realized that it was because of my custom icon.
below an exaple code:
from tkinter import *
from tkinter import ttk
class MainWindow:
def __init__(self):
self.parent=Tk()
self.parent.geometry("494x410+370+100")
self.parent.title("My Software - WITH ICON")
self.parent.iconbitmap("icon.ico")
Button = ttk.Button(self.parent, text="open a new widnow", command=self.OpenNewWindow)
Button.place(x=16, y=16)
def OpenNewWindow(self):
self.obj = NewWindow(self)
class NewWindow:
def __init__(self, mw):
self.window, self.mw = Toplevel(mw.parent), mw
self.window.geometry("200x150+360+200")
self.window.title("New Window")
self.window.iconbitmap("icon.ico") # it creates the issue..
self.window.protocol("WM_DELETE_WINDOW", self.on_close)
self.window.focus()
self.mw.parent.attributes('-disabled', 1)
self.window.transient(mw.parent)
self.window.grab_set()
self.mw.parent.wait_window(self.window)
def on_close(self):
self.mw.parent.attributes('-disabled', 0)
self.window.destroy()
def main():
app=MainWindow()
app.parent.mainloop()
if __name__=="__main__":
main()
to make it clear where is the issue, I create a GIF:
here we have two softwares, "without icon.py" and "with icon.py". they are the same, but the first one doesn't use my custom icon for his second window.
as you can see, if I run "with icon.py" the second window will be always affected by something when I will open it, but for "without icon.py" this "something" doesn't exist.
what is the issue? the software opens his second window, focus the root one (it's the issue), and then focus the second window again. you can see it clearly from the GIF.
how can I solve the issue? and why with the default icon this weird behaviour doesn't happen?
I would like to know how to do in python/Pyside2:
Create a QSystemTrayIcon with a custom icon, in which:
If I click left button on it, I do a custom action (just print “left click pressed”). No menu should be shown...
If I click right button on it, a context menu appears with an exit action on it, just to close the program.
On MacOS, maybe not in win nor linux, the menu just opens on mouse press... That's why the need of left and right click differentiations
otherwise both actions will be done with left and right click. See note here: On macOS... since the menu opens on mouse press
I need help just implementing the left and right click differentiations in the following code:
from PySide2 import QtWidgets
import sys
class SystrayLauncher(object):
def __init__(self):
w = QtWidgets.QWidget() #just to get the style(), haven't seen other way
icon = w.style().standardIcon(QtWidgets.QStyle.SP_MessageBoxInformation)
self.tray = QtWidgets.QSystemTrayIcon()
self.tray.setIcon(icon)
self.tray.setVisible(True)
self.tray.activated.connect(self.customAction)
# I JUST WANT TO SEE THE MENU WHEN RIGHT CLICK...
self.trayIconMenu = QtWidgets.QMenu()
self.quitAction = QtWidgets.QAction("&Quit", None, triggered=QtWidgets.QApplication.instance().quit)
self.trayIconMenu.addAction(self.quitAction)
self.tray.setContextMenu(self.trayIconMenu)
# JUST WANNA USE THE ACTION WITH LEFT CLICK
def customAction(self, signal):
print "left click pressed"
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
app.setQuitOnLastWindowClosed(False)
sl = SystrayLauncher()
sys.exit(app.exec_())
Can anyone help me, please?
you can differentiate the click "reason" and decide what to do. Therefore you will need to add a function like follows:
def right_or_left_click(reason):
if reason == QSystemTrayIcon.ActivationReason.Trigger:
print("Left-click detected")
elif reason == QSystemTrayIcon.ActivationReason.Context:
print("Right-click detected")
elif reason == QSystemTrayIcon.ActivationReason.MiddleClick:
print("Middle-click detected")
else:
print("Unknown reason")
self.tray.activated.connect(right_or_left_click)
Then, you can call the desired function on left-click or middle-click. The right-click is occupied by your context menu :)
I am developing a python plugin on QGIS using PyQT. My plugin contains a main window and a pop-up window which will be launched when clicking on a button on the main window.
My problem is when I relaunch again the plugin, the pop-up window will be displayed instead of the main window.
I found this on Stack Overflow (launch a PyQT window from a main PyQt window, and get the user input?) but it doesn't help me to solve my problem.
Can you help me ?
Thank you in advance.
EDIT add code
In my main window, I call to launch my pop-up window :
def show(self):
self.project.load()
if self.project.tranus_project:
self.tranus_folder.setText(self.project.tranus_project.path)
self.activate_launch_button()
self.launch_options_TRANUS()
super(OptionsTRANUSDialog, self).show()
......
def launch_options_TRANUS(self):
self.get_checked_list()
if self.checked_list != [] :
dialog = launch_tranus_dialog.LaunchTRANUSDialog(self.checked_list,self.folder,self.tranus_binaries,parent=self)
dialog.show()
result = dialog.exec_()
self.reinitialise_checked_list()
The class of my pop-up window is defined as :
class LaunchTRANUSDialog(QtGui.QDialog, FORM_CLASS):
def __init__(self,checked_list,project_directory,tranus_bin_path,parent=None):
"""Constructor."""
super(LaunchTRANUSDialog, self).__init__(parent)
self.setupUi(self)
self.project = parent.project
self.proj = QgsProject.instance()
self.tranus_bin_path = tranus_bin_path
self.project_directory = project_directory
self.project = parent.project
self.checked_list = checked_list
self.is_all_checked = False
self.tabs = self.findChild(QtGui.QTabWidget, 'tabWidget')
self.proj = QgsProject.instance()
#control actions
self.tabs.blockSignals(True)
self.tabs.currentChanged.connect(self.onChange)
self.tabs.blockSignals(False)
def show(self):
self.put_tabs()
super(LaunchTRANUSDialog, self).show()
I have solved my problem, in my main window in method show() I delete self.activate_launch_button()
self.launch_options_TRANUS()
So, I'm using Python and PyQt. I have a Main Window that contains a QTableWidget, and a dialog that opens modally and has some QLineEdit widgets... All right so far, but I have 2 problems:
When the dialog opens, my Main Window freezes, and I don't really like that...
What I want, when I finish editing a QLineEdit, is that the program will search the QTableWidget, and if the text from the QLineEdit exists in the table, a dialog will come up and informe about that. That's the general idea. But, so far, I seem to only be able to create a new QTableWidget instance, and I can't use the data from the existing...
What can I do about these?
You wrote:
and a dialog that opens modally
and then:
When the dialog opens, my Main Window freezes
The docs say:
int QDialog::exec () [slot]
Shows the dialog as a modal dialog,
blocking until the user closes it. The function returns a DialogCode
result. If the dialog is application modal, users cannot interact with
any other window in the same application until they close the dialog.
If the dialog is window modal, only interaction with the parent window
is blocked while the dialog is open. By default, the dialog is
application modal.
About modeless dialogs:
A modeless dialog is a dialog that operates independently of other
windows in the same application. Find and replace dialogs in
word-processors are often modeless to allow the user to interact with
both the application's main window and with the dialog.
Modeless
dialogs are displayed using show(), which returns control to the
caller immediately.
An example:
import sys
from PyQt4 import QtCore, QtGui
class SearchDialog(QtGui.QDialog):
def __init__(self, parent = None):
QtGui.QDialog.__init__(self, parent)
self.setWindowTitle('Search')
self.searchEdit = QtGui.QLineEdit()
layout = QtGui.QVBoxLayout()
layout.addWidget(self.searchEdit)
self.setLayout(layout)
class MainWindow(QtGui.QDialog):
def __init__(self):
QtGui.QDialog.__init__(self, None)
self.resize(QtCore.QSize(320, 240))
self.setWindowTitle('Main window')
self.logText = QtGui.QPlainTextEdit()
searchButton = QtGui.QPushButton('Search')
layout = QtGui.QVBoxLayout()
layout.addWidget(self.logText)
layout.addWidget(searchButton)
self.setLayout(layout)
searchButton.clicked.connect(self.showSearchDialog)
def showSearchDialog(self):
searchDialog = SearchDialog(self)
searchDialog.show()
searchDialog.searchEdit.returnPressed.connect(self.onSearch)
def onSearch(self):
self.logText.appendPlainText(self.sender().text())
def main():
app = QtGui.QApplication(sys.argv)
mainWindow = MainWindow()
mainWindow.show()
app.exec_()
if __name__ == "__main__":
main()
Click 'Search' to open a search window (you can open several of them). Enter a text to search and press Enter. The text to search will be added to the log in the main window.