PDA

View Full Version : Problem with connecting push button with font change



vuletic
9th December 2014, 11:45
Hello, I'm new to QT I'm trying to make a simple texteditor but I have problem with connecting push button to make my TextEdit change current font to Bold/Underline/Italic, this is how my program looks like at moment: http://i.imgur.com/5d6A82M.png

This is my mainwindow.cpp:


#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "mydialog.h"
#include "ui_mydialog.h"
#include <QLabel>
#include <QFontComboBox>
#include<QPushButton>
#include<QLineEdit>
#include <QTextEdit>


MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);

QTextEdit* myTextWindow = new QTextEdit;
setCentralWidget(myTextWindow);

QLabel* myLabelForFontComboBox = new QLabel;
ui->toolBar->addWidget(myLabelForFontComboBox);
myLabelForFontComboBox->setText("Font");

QFontComboBox* myFontComboBox = new QFontComboBox;
ui->toolBar->addWidget(myFontComboBox);
connect(myFontComboBox,SIGNAL(currentFontChanged(Q Font)),myTextWindow,SLOT(setCurrentFont(QFont)));//<---- this works


QPushButton* myBoldButton = new QPushButton;
myBoldButton->setMaximumWidth(20);
ui->toolBar->addWidget(myBoldButton);
myBoldButton->setStyleSheet("QPushButton {color: black; font: bold}");
myBoldButton->setText("B");
connect(myBoldButton,SIGNAL(clicked()),myTextWindo w,SLOT(setFontWeight(int)));// <--- this doesn't work


//myTextWindow->setFontWeight(QFont::Bold); <---- this works
//myTextWindow->setFontUnderline(QFont::UnderlineResolved);<---- this works
//myTextWindow->setFontItalic(QFont::StyleItalic);<---- this works


QPushButton* myItalicButton = new QPushButton;
myItalicButton->setMaximumWidth(20);
ui->toolBar->addWidget(myItalicButton);
myItalicButton->setStyleSheet("QPushButton {color: black; font: italic}");
myItalicButton->setText("I");
//connect(myItalicButton,SIGNAL(pressed()),myTextWin dow,SLOT(setFontItalic(bool))); <---- this doesn't work

QPushButton* myUnderLineButton = new QPushButton;
myUnderLineButton->setMaximumWidth(20);
ui->toolBar->addWidget(myUnderLineButton);
myUnderLineButton->setStyleSheet("QPushButton {color: black; font: bold}");
myUnderLineButton->setText("U");
// connect(myUnderLineButton,SIGNAL(toggled(bool)),my TextWindow,SLOT(setFontUnderline(bool))); <---- this doesn't work


QLineEdit* mySearchSpace = new QLineEdit;
mySearchSpace->setMaximumWidth(100);
ui->toolBar->addWidget(mySearchSpace);


}
MainWindow::~MainWindow()
{
delete ui;
}

void MainWindow::on_actionPreference_triggered()
{
MyDialog Option;
Option.setModal(true);
Option.exec();
}


and this is my mainwindow.h:


#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>


namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
Q_OBJECT

public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();

private slots:
void on_actionPreference_triggered();

private:
Ui::MainWindow *ui;

};

#endif // MAINWINDOW_H



Any advice on this matter or anything else concerning my code, is appreciated.
Thanks.

Lesiok
9th December 2014, 11:52
Generally You can not connect signal without parameter to slot with parameter.

Radek
9th December 2014, 12:32
That's why most of your connect()'s does not work. The last connect() does not work, IMO, because toggled() signal is defined only for "checkable" buttons (radio buttons, check boxes). If you want a real "toggle button" then make your push button "checkable" first:


myUnderLinebutton->setCheckable(true);

myUnderLineButton will behave like toggle button. Now, connect() the toggle() signal. Similarly, you can make other buttons (bold, italic) toggle buttons, too.

vuletic
9th December 2014, 15:30
Hello, Lesiok thanks for that info, and Radek thank you for your solution it works (for underline and italic that is) but for Bold I have problem:

Seems like for some reason toggle is not compatible with "setFontWeight(int)" :

Object::connect: Incompatible sender/receiver arguments
QPushButton::toggled(bool) --> QTextEdit::setFontWeight(int)


connect(myBoldButton,SIGNAL(toggled(bool)),myTextW indow,SLOT(setCurrentFont(QFont::Bold)))<-- nor this

Radek
9th December 2014, 18:28
The same as Lesiok has written. You cannot have a bool parameter of the signal and an int parameter of the corresponding slot. Write your own slot for the font weight (with a bool param) and call myTextWindow->setFontWeight(the bool param ?QFont::Bold : QFont::Normal) from it. You can call slots like normal procedures yourself.

Naturally, myTextWindow needs to become a data member of MainWindow. It must not be local in MainWindow ctor. Such a pointer is lost once you leave the ctor.

jakr13
10th December 2014, 15:27
What Radek said is exactly the solution, you must write your own slot for the font weight.

cheers!!!

vuletic
11th December 2014, 12:08
Yeah I made changes, thanks all, and it works very well now, but what I'm wondering now is it possible to make it look "unchecked" on my window like (blue in this picture) http://i.imgur.com/RZPu5cb.png and not red.

jakr13
11th December 2014, 12:51
http://qt-project.org/forums/viewthread/5482

check this thread out. I think it is what you are looking for!!!

cheers!!!