-1

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"))

This is the program environment. And with the browse button, the user can select the file. And the path of that file is written inside that QLineEdit. And by pressing the next button, if a path was entered, it will enter a loading page, and if it is not there, the text of QLineEdit will be changed to an invalid address or something like that.

In short, how to measure the length of the string entered into QLineEdit?

  • Not sure I completely understand the question but... can't you just check the length of the string returned by [`QLineEdit::text`](https://doc.qt.io/qt-6/qlineedit.html#text-prop)? – G.M. Dec 17 '22 at 11:16
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Dec 17 '22 at 15:59
  • @G.M. I want to get the length of the string inside QLineEdit. how to measure the length of the string entered into QLineEdit? – parham esmailzadeh Dec 17 '22 at 17:07
  • 1
    @parhamesmailzadeh QLineEdit is a widget, using `str()` with it doesn't make any sense at all. *Study* the [documentation](//doc.qt.io/qt-5/qlineedit.html) and refer to the widget's properties. Also: 1. don't use globals; 2. checking the result of the QFileDialog static function is wrong, as it will always be a truth value (it's a tuple); 3. don't try to mimic/edit/merge pyuic generated code without knowing what you're doing (`MainWindow` is used as a global reference, which is wrong); 5. learn to use [layout managers](//doc.qt.io/qt-5/designer-layouts.html) and avoid fixed geometries. – musicamante Dec 17 '22 at 18:18

1 Answers1

0

In your Next method do.

    def Next(self):
        global Video_Address
        text = self.lineEdit.text()
        if len(text) <= 1:
            self.lineEdit.setPlaceholderText("Invalid Directory...")
        ...
        ...
Alexander
  • 16,091
  • 5
  • 13
  • 29
  • Thanks, it doesn't give an error anymore, but when I click on the next button, the program closes without any error. What should be done to fix this? – parham esmailzadeh Dec 17 '22 at 18:27
  • @parhamesmailzadeh That is because you are calling `self.close()` that is the reason why the program closes because you tell it too – Alexander Dec 17 '22 at 18:30