Issue
I am writing a project that comes and takes the file address(path) from the user. And when the user clicks on the next button, she enters the loading page. I encountered a problem here that when the user does not specify the address and path of the file, she enters the loading page. And I want to write to QLineEdit when the user clicks the button without specifying the file path, please specify the path or something like that.
And I feel that for this purpose the length of QlineEdit should be checked and if it is less than one, it should write the text for the user, but the problem is that there is no function that measures the length of QLineEdit values.
First, I wanted to convert QLineEdit to a string, which Python does not allow such a thing in Python.
This is Source Of Project:
from PyQt5.QtWidgets import QMainWindow, QApplication, QPushButton , QFileDialog, QWidget, QLineEdit, QStatusBar, QMenuBar
from PyQt5.QtCore import QRect, QCoreApplication, QMetaObject
from PyQt5.QtGui import QFont
import sys
import time
class Ui(QMainWindow):
def __init__(self):
super(Ui, self).__init__()
MainWindow.setObjectName("MainWindow")
MainWindow.setEnabled(True)
MainWindow.resize(577, 287)
self.setWindowTitle("Browse Your File")
self.centralwidget = QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.lineEdit = QLineEdit(self.centralwidget)
self.lineEdit.setGeometry(QRect(20, 40, 541, 61))
font = QFont()
font.setPointSize(16)
font.setBold(True)
font.setWeight(75)
self.lineEdit.setFont(font)
self.lineEdit.setStyleSheet("QLineEdit {\n"
"background: #93deed;\n"
"border: 2px solid rgb(0 , 170 , 255);\n"
"border-radius: 30px ;\n"
"color: white;\n"
"}\n"
"\n"
"QLineEdit:focus {\n"
" border: 2px solid #DA7B93;\n"
"}")
self.lineEdit.setObjectName("lineEdit")
self.pushButton = QPushButton(self.centralwidget)
self.pushButton.setGeometry(QRect(50, 160, 151, 61))
font = QFont()
font.setPointSize(16)
font.setBold(True)
font.setWeight(75)
self.lineEdit.setReadOnly(True)
self.pushButton.setFont(font)
self.pushButton.setStyleSheet("QPushButton {\n"
" background: #2F4454;\n"
" border: 2px solid #2F4454;\n"
" border-radius: 25px;\n"
" color:white;\n"
"}\n"
"\n"
"QPushButton:hover {\n"
" background-color: #DA7B93;\n"
" border: 2px solid #DA7B93;\n"
"}\n"
"")
self.pushButton.setObjectName("pushButton")
self.nextBtn = QPushButton(self.centralwidget)
self.nextBtn.setGeometry(QRect(350, 160, 151, 61))
font = QFont()
font.setPointSize(16)
font.setBold(True)
font.setWeight(75)
self.nextBtn.setFont(font)
self.nextBtn.setStyleSheet("QPushButton {\n"
" background: #2F4454;\n"
" border: 2px solid #2F4454;\n"
" border-radius: 25px;\n"
" color:white;\n"
"}\n"
"\n"
"QPushButton:hover {\n"
" background-color: #DA7B93;\n"
" border: 2px solid #DA7B93;\n"
"}\n"
"")
self.pushButton.setObjectName("nextBtn")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QMenuBar(MainWindow)
self.menubar.setGeometry(QRect(0, 0, 577, 26))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QMetaObject.connectSlotsByName(MainWindow)
self.pushButton.clicked.connect(self.Clicker)
self.nextBtn.clicked.connect(self.Next)
self.nextBtn.clicked.connect(MainWindow.close)
def Clicker(self):
# Open File:
global fname
fname = QFileDialog.getOpenFileName(self, "Open File", "", "Video Files (*.mp4);;Video Files (*.mov);;Video Files (*.avi);;Video Files (*.mkv)")
# Output File DIR to Screen:
if fname:
self.lineEdit.setText(" "+str(fname[0]))
def Next(self):
global Video_Address
lineEditNew = str(self.lineEdit)
if (self.lineEdit) <= 1:
self.lineEdit.setPlaceholderText("Invalid Directory...")
else:
print(str(fname))
Video_Address = str(fname[0])
self.close()
self.loading_screen = SplashScreen()
self.loading_screen.show()
def retranslateUi(self, MainWindow):
_translate = QCoreApplication.translate
MainWindow.setWindowTitle(_translate("Browse Your File", "Browse Your File"))
self.lineEdit.setPlaceholderText(_translate("MainWindow", " Directory Of Your Video..."))
self.pushButton.setText(_translate("MainWindow", "Browse"))
self.nextBtn.setText(_translate("MainWindow", "Next"))
In short, how to measure the length of the string entered into QLineEdit?
Solution
In your Next
method do.
def Next(self):
global Video_Address
text = self.lineEdit.text()
if len(text) <= 1:
self.lineEdit.setPlaceholderText("Invalid Directory...")
...
...
Answered By - Alexander
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.