Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How to link signal and slot when i change the font in the dialog



  • This is how i am currently choosing the font in my application.

    ![7f1b3861-0298-4d5d-9a6f-952a0bf84303-image.png]FontPreview.png
    The application uses this code to open the font diaglog

    void FontChange()
    {
    
    	QString filePath = QFileDialog::getOpenFileName(NULL, tr("Open File"),
    		QStandardPaths::standardLocations(QStandardPaths::FontsLocation)[0],
    		tr("Fonts (*.ttf);;Everything (*.*)"), nullptr,
    		QFileDialog::DontUseNativeDialog);
    
    	if (filePath.isEmpty())
    		return;
    
    	QlineEditSetFont->setText(filePath);
    	stdstrLocation = filePath.toStdString();
    	this->isChanged = true; // this executes the function
    }
    I want to execute a function whenever i change my selection in the Dialog , currently i  have to choose a font and than click on open to execute the function
    


  • Hi, excuse me, but why don't you use QFontDialog to choose font?



  • @Bonnie said in How to link signal and slot when i change the font in the dialog:

    QFontDialog

    @Bonnie i am having a look into this option.



  • @Bonnie It does not work for me as i need to use this font for opengl rendering and i need the path of the font along with its actual name.



  • And what you need is something like a "live dialog".
    So you cannot use the QFontDialog ::getFont static function because it is modal.
    I would suggest doing something like:

    QFontDialog *fd = new QFontDialog(this);
    fd->setAttribute(Qt::WA_DeleteOnClose);
    fd->setOption(QFontDialog::NoButtons);
    fd->show();
    connect(fd, &QFontDialog::currentFontChanged, [=](const QFont &font){
        qDebug() << font;
    });
    

    [ADDED]
    Even though you cannot use QFontDialog, but the theory is the same.
    You cannot use the static function.
    You need to create a dialog, and connect the current*Changed signal.



  • @Bonnie said in How to link signal and slot when i change the font in the dialog:

    QFontDialog *fd = new QFontDialog(this);
    fd->setAttribute(Qt::WA_DeleteOnClose);
    fd->setOption(QFontDialog::NoButtons);
    fd->show();
    connect(fd, &QFontDialog::currentFontChanged, [=](const QFont &font){
    qDebug() << font;
    });

    @Bonnie thank you , i get your point and will try to use file open with a object.



  • @Bonnie said in How to link signal and slot when i change the font in the dialog:

    modal

    @Bonnie how can i print the file path in this code

    QFileDialog* dialog = new QFileDialog();	
    dialog->setAttribute(Qt::WA_DeleteOnClose);
    dialog->setNameFilter("TTF (*.ttf)");
    dialog->setOption(QFileDialog::DontUseNativeDialog);
    dialog->setDirectory("C:\\Windows\\Fonts");
    dialog->show();
    	
    QObject::connect(dialog, &QFileDialog::currentChanged, [=]() {
    		qDebug() << "Changed" << // print the current path; });
    


  • @summit

    QObject::connect(dialog, &QFileDialog::currentChanged, [=](const QString &path) {
        qDebug() << path;
    });
    

    And i've thought about this again, a modal dialog is also usable.

    QFileDialog dialog;
    dialog.setNameFilter("TTF (*.ttf)");
    dialog.setOption(QFileDialog::DontUseNativeDialog);
    dialog.setDirectory("C:\\Windows\\Fonts");
    //if using a modal dialog, the "connect" should be placed before "exec()"
    QObject::connect(&dialog, &QFileDialog::currentChanged, [=](const QString &path) {
        qDebug() << path;
    });
    dialog.exec();
    

    So it is up to you whether you would like a modal or modaless one.



  • @Bonnie said in How to link signal and slot when i change the font in the dialog:

    QObject::connect(&dialog, &QFileDialog::currentChanged, [=](const QString &path) {
    qDebug() << path;
    });

    @Bonnie Thank you very much this is what i need.


Log in to reply