pyqt5 - Problem switching back from QDialog window to QMainWindow - pyqt5

Hope indention is OK. I removed the reference to the current instance but the issue is still there. Closing the QDialog window with 'x' the MainWindow is displayed for a second and then it close out, while pressing the 'Cancel' button in the QDialog window it works fine.
Thanks for any suggestion
'''
import sys
from PyQt5 import QtWidgets, QtCore
from PyQt5.QtCore import QSize, Qt
from PyQt5.QtWidgets import QApplication,QWidget, QMainWindow, QDialog
from MainW import Ui_f_main
from ShowDialogUi import Ui_f_Dialog
class MainWindow(QWidget, Ui_f_main):
def __init__(self):
super().__init__()
self.setupUi(self)
self.wDiag = None
self.pbDialog.clicked.connect(self.dialog)
def dialog(self):
if self.wDiag is None:
window.close()
self.wDiag=ShowDialog()
self.wDiag.setAttribute(Qt.WA_DeleteOnClose)
self.wDiag.destroyed.connect(self.resetFlag_wDiag)
self.wDiag.show()
self.wDiag.pbOK.clicked.connect(self.go_on)
self.wDiag.pbCancel.clicked.connect(self.go_back)
else:
self.wDiag.close()
self.wDiag=None
def go_on(self):
print("OK")
def go_back(self):
window.show()
self.wDiag.close()
self.wDiag=None
def resetFlag_wDiag(self):
window.show()
self.wDiag=None
class ShowDialog(QDialog,Ui_f_Dialog):
def __init__(self):
super().__init__()
self.setupUi(self)
app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec_())
'''

Related

How to count lines in qscintilla?

I'm using QScintilla to make my own notepad for fun in pyqt5 python. I was wandering if there is a way to get the number of lines of a QScintilla() widget?
You have to use the lines() method, you can also use the linesChanged signal.
import sys
from PyQt5 import QtWidgets, Qsci
class Editor(Qsci.QsciScintilla):
def __init__(self, parent=None):
super().__init__(parent)
self.setText("Foo\nBar")
self.print_lines()
self.linesChanged.connect(self.handle_lines_changed)
def handle_lines_changed(self):
self.print_lines()
def print_lines(self):
print("total lines: {}".format(self.lines()))
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
w = Editor()
w.resize(640, 480)
w.show()
sys.exit(app.exec_())

PyQt5 window opens then closes in a second

from PyQt5.QtWidgets import QApplication, QPushButton, QMainWindow
from PyQt5.QtGui import QIcon
import sys
class Main(QMainWindow):
def __init__(self, parent=None) -> None:
super().__init__(parent=parent)
self.setWindowTitle("QPushButton")
left = 300
top = 200
width = 300
height = 250
self.setGeometry(left, top, width, height)
self.show()
def main():
App = QApplication(sys.argv)
Main()
sys.exit(App.exec())
if __name__=="__main__":
main()
I don't get it, it worked before now it closes within a second of opening. I'm thinking somehow my installation got corrupted. Do you have any ideas I tried
def main():
App = QApplication(sys.argv)
Main()
App.exec_()

How to connect using menubar in pyqt5?

I want to open a picture when I click the menu, but I don't know how
I want to be able to output the specified image file when clicking the submenu.
import sys
from PyQt5.QtWidgets import QMainWindow, QMenu, QApplication, QLabel
from PyQt5.QtGui import QPixmap, QIcon,QAction,*
class Example(QMainWindow):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
menubar= self.menubar()
self.filemenu = menubar.addmenu('a')
x= QMenu('b',self)
y= QMenu('c',self)
self.filemenu.addmenu(y)
self.x1 =QAction('d',self)
x.addMenu(self.x1)
self.x1.triggered.connect(self.fileopen)
def fileopen(self):
qpixmapvar=QPixmap()
qpixmapvar.load("C:\\desktop\\A.png")
self.label.setPixmap(qPixmapvar)
self.label.resize(qPixmapvar.width(), qPixmapvar.height())
if__name__ =='__main__':
app= Qapplication(sys.argv)
ex = Example()
sys.exit(app.exec())

How do I prevent the QPushButton.clicked from the widget from executing when I'm in MainWindow pyqt5?

I set the printInWidget clicked method for the btnprint button in widget1. But I want to replace that command in MainWindow with command printInMain by code: self.wid.btnprint.clicked.connect(self.printInMain). I don't know why it does both commands in MainWindow. Please help me make the command printInWidget not execute when running MainWidow.
Sorry everyone (English is not my native language and I only approached pyqt5 for a few months by teaching myself).
when I click button "printText"
my code:
mainwindow.py
from PyQt5 import QtWidgets
from PyQt5.QtWidgets import QMainWindow, QApplication, QPushButton, QTextEdit, QGridLayout, QVBoxLayout, QFileDialog,QHBoxLayout,QSpacerItem,QSizePolicy
from PyQt5 import uic, QtCore
import sys
from widget1 import *
class UI(QMainWindow):
def __init__(self):
super(UI, self).__init__()
uic.loadUi("MainWindow.ui", self)
self.wid = widget1()
self.verticalLayout.addWidget(self.wid)
self.wid.btnprint.clicked.connect(self.printInMain)
# I want to override method of button printText
def printInMain(self):
self.wid.labelB.setText('New text (method in Main)')
if __name__ == '__main__':
app = QApplication(sys.argv)
window = UI()
window.show()
app.exec_()
widget1.py
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QMainWindow
from PyQt5.QtGui import QIcon
from PyQt5 import uic
class widget1(QWidget):
def __init__(self):
super().__init__()
uic.loadUi('widget1.ui',self)
self.btnprint.clicked.connect(self.printInwidget)
def printInwidget(self):
self.labelA.setText('Hello World (Method in Widget)')
if __name__ == '__main__':
app = QApplication(sys.argv)
Main = QMainWindow()
widget = widget1()
widget.show()
sys.exit(app.exec_())
You need to disconnect. If you want to use the initial behaviour again you need to connect the signal to the slot again. I would suggest creating a reconnect() function that will do that for you if you decide to do the whole thing many times.
Here is an example.
More info can be found in the official QObject documentation (look for bool QObject::disconnect(const QObject *sender, const char *signal, const QObject *receiver, const char *method)).
Also try to provide full code or at least a minimal working example. What you have posted right now cannot work due to missing dependencies and a UI file.

pyqt5 videowidget not showing in layout

I am writing a program with pyqt5 where pressing a button first cycles through some pictures then cycles through some videos.
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtMultimedia import *
from PyQt5.QtMultimediaWidgets import *
import glob
import argparse
import sys
class MainWindow(QMainWindow):
def __init__(self,args):
super(MainWindow, self).__init__()
self.setWindowTitle('Navon test')
self.setWindowFlags(Qt.FramelessWindowHint)
# exit option for the menu bar File menu
self.exit = QAction('Exit', self)
self.exit.setShortcut('Ctrl+q')
# message for the status bar if mouse is over Exit
self.exit.setStatusTip('Exit program')
# newer connect style (PySide/PyQT 4.5 and higher)
self.exit.triggered.connect(app.quit)
self.setWindowIcon(QIcon('icon.ico'))
self.centralwidget = CentralWidget(args)
self.setCentralWidget(self.centralwidget)
def keyPressEvent(self, QKeyEvent):
if QKeyEvent.key() == Qt.Key_Escape:
QCoreApplication.instance().quit()
self.centralwidget.startvid()
class CentralWidget(QWidget):
def __init__(self,args):
super(CentralWidget, self).__init__()
self.layout = QVBoxLayout()
self.layout.setAlignment(Qt.AlignCenter)
self.setLayout(self.layout)
self.player = QMediaPlayer(None, QMediaPlayer.VideoSurface)
self.vw = QVideoWidget()
self.player.setVideoOutput(self.vw)
def startvid(self):
self.layout.addWidget(self.vw)
url= QUrl.fromLocalFile(glob.glob("videos/*")[0])
content= QMediaContent(url)
self.player.setMedia(content)
self.player.setVideoOutput(self.vw)
self.player.play()
if __name__== "__main__":
parser = argparse.ArgumentParser()
#~ parser.add_argument("-nb","--nobox",action="store_true", help="do not wait for the box connection")
args = parser.parse_args()
app = QApplication(sys.argv)
mainwindow = MainWindow(args)
#~ mainwindow.showFullScreen()
mainwindow.show()
sys.exit(app.exec_())
I tried to paste the minimal code. The thing is, I press the button nothing shows, although I used examples like this one PyQt5 - Can't play video using QVideoWidget to test if playing the video is ok, and these work. It's as if it is not adding the widget to the layout or something. Any idea what might be wrong?
I had to use QGraphicsView to achieve what I wanted, here is a fix:
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtMultimedia import *
from PyQt5.QtMultimediaWidgets import *
import glob
class MainWindow(QMainWindow):
def __init__(self):
super(MainWindow, self).__init__()
self.setWindowTitle('Navon test')
self.setWindowFlags(Qt.FramelessWindowHint)
# exit option for the menu bar File menu
self.exit = QAction('Exit', self)
self.exit.setShortcut('Ctrl+q')
# message for the status bar if mouse is over Exit
self.exit.setStatusTip('Exit program')
# newer connect style (PySide/PyQT 4.5 and higher)
self.exit.triggered.connect(app.quit)
self.setWindowIcon(QIcon('icon.ico'))
self.centralwidget = VideoPlayer()
self.setCentralWidget(self.centralwidget)
def keyPressEvent(self, QKeyEvent):
if QKeyEvent.key() == Qt.Key_Escape:
self.centralwidget.phaseQuit(2)
self.centralwidget.play()
class VideoPlayer(QWidget):
def __init__(self, parent=None):
super(VideoPlayer, self).__init__(parent)
self.mediaPlayer = QMediaPlayer(None, QMediaPlayer.VideoSurface)
self.videoItem = QGraphicsVideoItem()
self.videoItem.setSize(QSizeF(640, 480))
scene = QGraphicsScene(self)
graphicsView = QGraphicsView(scene)
scene.addItem(self.videoItem)
layout = QVBoxLayout()
layout.addWidget(graphicsView)
self.setLayout(layout)
self.mediaPlayer.setVideoOutput(self.videoItem)
self.counter = 0
def play(self):
if self.mediaPlayer.state() == QMediaPlayer.PlayingState:
pass
else:
self.mediaPlayer.setMedia(QMediaContent(QUrl.fromLocalFile(glob.glob("videos/*")[self.counter])))
self.mediaPlayer.play()
self.counter += 1
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
player = MainWindow()
player.show()
sys.exit(app.exec_())