Results 1 to 8 of 8

Thread: Problem with connecting push button with font change

  1. #1
    Join Date
    Dec 2014
    Posts
    29
    Thanks
    12
    Qt products
    Qt5 PyQt3 PyQt4
    Platforms
    Unix/X11 Windows

    Default Problem with connecting push button with font change

    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:
    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3. #include "mydialog.h"
    4. #include "ui_mydialog.h"
    5. #include <QLabel>
    6. #include <QFontComboBox>
    7. #include<QPushButton>
    8. #include<QLineEdit>
    9. #include <QTextEdit>
    10.  
    11.  
    12. MainWindow::MainWindow(QWidget *parent) :
    13. QMainWindow(parent),
    14. ui(new Ui::MainWindow)
    15. {
    16. ui->setupUi(this);
    17.  
    18. QTextEdit* myTextWindow = new QTextEdit;
    19. setCentralWidget(myTextWindow);
    20.  
    21. QLabel* myLabelForFontComboBox = new QLabel;
    22. ui->toolBar->addWidget(myLabelForFontComboBox);
    23. myLabelForFontComboBox->setText("Font");
    24.  
    25. QFontComboBox* myFontComboBox = new QFontComboBox;
    26. ui->toolBar->addWidget(myFontComboBox);
    27. connect(myFontComboBox,SIGNAL(currentFontChanged(QFont)),myTextWindow,SLOT(setCurrentFont(QFont)));//<---- this works
    28.  
    29.  
    30. QPushButton* myBoldButton = new QPushButton;
    31. myBoldButton->setMaximumWidth(20);
    32. ui->toolBar->addWidget(myBoldButton);
    33. myBoldButton->setStyleSheet("QPushButton {color: black; font: bold}");
    34. myBoldButton->setText("B");
    35. connect(myBoldButton,SIGNAL(clicked()),myTextWindow,SLOT(setFontWeight(int)));// <--- this doesn't work
    36.  
    37.  
    38. //myTextWindow->setFontWeight(QFont::Bold); <---- this works
    39. //myTextWindow->setFontUnderline(QFont::UnderlineResolved);<---- this works
    40. //myTextWindow->setFontItalic(QFont::StyleItalic);<---- this works
    41.  
    42.  
    43. QPushButton* myItalicButton = new QPushButton;
    44. myItalicButton->setMaximumWidth(20);
    45. ui->toolBar->addWidget(myItalicButton);
    46. myItalicButton->setStyleSheet("QPushButton {color: black; font: italic}");
    47. myItalicButton->setText("I");
    48. //connect(myItalicButton,SIGNAL(pressed()),myTextWindow,SLOT(setFontItalic(bool))); <---- this doesn't work
    49.  
    50. QPushButton* myUnderLineButton = new QPushButton;
    51. myUnderLineButton->setMaximumWidth(20);
    52. ui->toolBar->addWidget(myUnderLineButton);
    53. myUnderLineButton->setStyleSheet("QPushButton {color: black; font: bold}");
    54. myUnderLineButton->setText("U");
    55. // connect(myUnderLineButton,SIGNAL(toggled(bool)),myTextWindow,SLOT(setFontUnderline(bool))); <---- this doesn't work
    56.  
    57.  
    58. QLineEdit* mySearchSpace = new QLineEdit;
    59. mySearchSpace->setMaximumWidth(100);
    60. ui->toolBar->addWidget(mySearchSpace);
    61.  
    62.  
    63. }
    64. MainWindow::~MainWindow()
    65. {
    66. delete ui;
    67. }
    68.  
    69. void MainWindow::on_actionPreference_triggered()
    70. {
    71. MyDialog Option;
    72. Option.setModal(true);
    73. Option.exec();
    74. }
    To copy to clipboard, switch view to plain text mode 
    and this is my mainwindow.h:
    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include <QMainWindow>
    5.  
    6.  
    7. namespace Ui {
    8. class MainWindow;
    9. }
    10.  
    11. class MainWindow : public QMainWindow
    12. {
    13. Q_OBJECT
    14.  
    15. public:
    16. explicit MainWindow(QWidget *parent = 0);
    17. ~MainWindow();
    18.  
    19. private slots:
    20. void on_actionPreference_triggered();
    21.  
    22. private:
    23. Ui::MainWindow *ui;
    24.  
    25. };
    26.  
    27. #endif // MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 

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

  2. #2
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problem with connecting push button with font change

    Generally You can not connect signal without parameter to slot with parameter.

  3. The following user says thank you to Lesiok for this useful post:

    vuletic (9th December 2014)

  4. #3
    Join Date
    Apr 2013
    Location
    Prague
    Posts
    258
    Thanks
    3
    Thanked 65 Times in 59 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Problem with connecting push button with font change

    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:
    Qt Code:
    1. myUnderLinebutton->setCheckable(true);
    To copy to clipboard, switch view to plain text mode 
    myUnderLineButton will behave like toggle button. Now, connect() the toggle() signal. Similarly, you can make other buttons (bold, italic) toggle buttons, too.

  5. The following user says thank you to Radek for this useful post:

    vuletic (9th December 2014)

  6. #4
    Join Date
    Dec 2014
    Posts
    29
    Thanks
    12
    Qt products
    Qt5 PyQt3 PyQt4
    Platforms
    Unix/X11 Windows

    Default Re: Problem with connecting push button with font change

    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

  7. #5
    Join Date
    Apr 2013
    Location
    Prague
    Posts
    258
    Thanks
    3
    Thanked 65 Times in 59 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Problem with connecting push button with font change

    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.

  8. The following user says thank you to Radek for this useful post:

    vuletic (9th December 2014)

  9. #6
    Join Date
    Jan 2013
    Location
    Bangalore, India
    Posts
    36
    Thanks
    9
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Problem with connecting push button with font change

    What Radek said is exactly the solution, you must write your own slot for the font weight.

    cheers!!!

  10. #7
    Join Date
    Dec 2014
    Posts
    29
    Thanks
    12
    Qt products
    Qt5 PyQt3 PyQt4
    Platforms
    Unix/X11 Windows

    Default Re: Problem with connecting push button with font change

    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.

  11. #8
    Join Date
    Jan 2013
    Location
    Bangalore, India
    Posts
    36
    Thanks
    9
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Problem with connecting push button with font change

    http://qt-project.org/forums/viewthread/5482

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

    cheers!!!

  12. The following user says thank you to jakr13 for this useful post:

    vuletic (11th December 2014)

Similar Threads

  1. Use Push Button to change a variable
    By Qem1st in forum Newbie
    Replies: 4
    Last Post: 17th January 2013, 22:03
  2. Problem with push button navigation
    By keyurparekh in forum Qt Programming
    Replies: 10
    Last Post: 19th July 2011, 12:18
  3. Problem with push button navigation.
    By keyurparekh in forum Qt Programming
    Replies: 2
    Last Post: 18th June 2011, 08:11
  4. How to change text color of push button?
    By augusbas in forum Qt Programming
    Replies: 2
    Last Post: 3rd July 2009, 10:32
  5. Push Button problem!!
    By Seema Rao in forum Qt Programming
    Replies: 1
    Last Post: 22nd April 2006, 16:31

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.