Results 1 to 1 of 1

Thread: [Solved] QToolButton Dynamic translation

  1. #1
    Join Date
    Jun 2014
    Posts
    1
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default [Solved] QToolButton Dynamic translation

    Hi everyone!

    I am writing a QT C++ application which uses a dynamic translation. It works fine with QPushButton, but it crashes when I try to dynamically translate QToolButton.
    Could anyone point me in the right direction to solve this problem?

    Declaration (mainwindow.hpp):

    Qt Code:
    1. #include <QMainWindow>
    2.  
    3.  
    4. class MainWindow : public QMainWindow {
    5. Q_OBJECT
    6.  
    7. public:
    8. explicit MainWindow(QMainWindow *parent = 0);
    9.  
    10. public slots:
    11.  
    12. private slots:
    13. void setLangEn();
    14. void setLangSl();
    15.  
    16. private:
    17. void closeEvent(QCloseEvent *event);
    18. void changeEvent(QEvent *event);
    19.  
    20. QPushButton *cancelBtn;
    21.  
    22. QToolButton *btnTParcelTake;
    23.  
    24. };
    To copy to clipboard, switch view to plain text mode 

    CODE (mainwindow.cpp):

    Qt Code:
    1. #include <QtGui>
    2. #include <QtWidgets>
    3. #include <QTranslator>
    4.  
    5. #include "main.h"
    6. #include "mainwindow.h"
    7. ...
    8.  
    9. MainWindow::MainWindow(QMainWindow *parent) : QMainWindow(parent) {
    10. ...
    11.  
    12. cancelBtn = new QPushButton(tr("Cancel"));
    13. cancelBtn->setObjectName("textBtn");
    14. cancelBtn->setVisible(false);
    15. connect(cancelBtn, SIGNAL(clicked()), this, SLOT(calcelCurrentProccess()));
    16.  
    17. QToolButton *btnTParcelTake = new QToolButton();
    18. btnTParcelTake->setText(tr("Take Package"));
    19. btnTParcelTake->setObjectName("choice");
    20. btnTParcelTake->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
    21. btnTParcelTake->setIcon(QIcon(QPixmap(":img/btn-parcel-take.png")));
    22. btnTParcelTake->setIconSize(QSize(248, 248));
    23. connect(btnTParcelTake, SIGNAL(clicked()), sm, SLOT(map()));
    24. sm->setMapping(btnTParcelTake, 1);
    25.  
    26. ...
    27. }
    28.  
    29. void MainWindow::setLangEn() {
    30. qApp->removeTranslator(&appTranslator);
    31. if (appTranslator.load("rc/translations/translation_en.qm", ".")) {
    32. qWarning("Translating to EN");
    33. qApp->installTranslator(&appTranslator);
    34. }
    35. }
    36.  
    37. void MainWindow::setLangSl() {
    38. qApp->removeTranslator(&appTranslator);
    39. if (appTranslator.load("rc/translations/translation_sl.qm", ".")) {
    40. qWarning("Translating to SLO");
    41. qApp->installTranslator(&appTranslator);
    42. }
    43. }
    44.  
    45. void MainWindow::changeEvent(QEvent *event) {
    46. if (event->type() == QEvent::LanguageChange) {
    47. setWindowTitle(tr("Translated"));
    48. cancelBtn->setText(tr("Cancel"));
    49. btnTParcelTake->setText(tr("Take Package")); //<<= program crashes when I use this command
    50. }
    51. }
    52.  
    53. ...
    To copy to clipboard, switch view to plain text mode 

    Thanks a lot!


    Added after 10 minutes:


    Ok, I have managed to solve this by changing QToolButton *btnTParcelTake = new QToolButton(); to btnTParcelTake = new QToolButton();
    Last edited by Osper; 30th June 2014 at 12:19.

Similar Threads

  1. Replies: 1
    Last Post: 10th February 2011, 20:32
  2. Problem with dynamic translation
    By keo in forum Qt Programming
    Replies: 2
    Last Post: 29th July 2009, 10:36
  3. Dynamic translation for ComboBox
    By Rakesh_Kumar in forum Qt Programming
    Replies: 1
    Last Post: 24th February 2009, 11:27
  4. How avoid translation of dynamic properties
    By thomas_-_s in forum Qt Tools
    Replies: 1
    Last Post: 19th October 2008, 22:02
  5. Replies: 27
    Last Post: 16th May 2008, 07:33

Tags for this Thread

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.