Page 1 of 2 12 LastLast
Results 1 to 20 of 32

Thread: Properly adding custom widget into Qt Designer

  1. #1
    Join Date
    Jan 2015
    Posts
    22
    Thanks
    3
    Qt products
    Qt5
    Platforms
    Windows

    Default Properly adding custom widget into Qt Designer

    Hello,

    I'm new to Qt, and I've been experimenting with a few things. I'm stuck with adding custom widgets to Qt Designer. I used the example given in Qt 5.3 (D:\Qt\Qt5.3.0\Examples\Qt-5.3\designer\customwidgetplugin), and ran it with release mode. Qt showed the 'find custom executable' dialogbox, which I think is fine (it's plugin anyway). Now, there are a few lines of command like:

    Qt Code:
    1. TARGET = $$qtLibraryTarget($$TARGET)
    2.  
    3. target.path = $$[QT_INSTALL_PLUGINS]/designer
    4. INSTALLS += target
    To copy to clipboard, switch view to plain text mode 

    I can't find the DLL installed in any 'designer' folder on my installation directory. The custom widget doesn't show up on the left side on Designer either, or pops up when I try to promote a blank widget. When I tried to put the resulting DLL into D:\Qt\Qt5.3.0\Tools\QtCreator\bin\plugins\designer , the designer plugins dialogbox shows that the plugin failed because it used an incompatible library ([5.3.0] release).

    Is there anything that I missed?

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,013 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Properly adding custom widget into Qt Designer

    You need to build the plugin with the same compiler, Qt version and same mode (release) Creator was built with. This might not be the same version you are using for building your programs.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Jan 2015
    Posts
    22
    Thanks
    3
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Properly adding custom widget into Qt Designer

    Quote Originally Posted by wysota View Post
    You need to build the plugin with the same compiler, Qt version and same mode (release) Creator was built with. This might not be the same version you are using for building your programs.
    Yes, I have tried to check with this answer. The problem is, I have never installed another version of Qt before on my machine. Another computer (also installed with Qt 5.3.0 and definitely compiled with mingw) also has the same problem. I used the same source code without any alteration, ran qmake, ran it with release mode, and when I put the resulting .a and .dll files into the plugins/designer folder the same error popped out. The custom widget won't appear on QtCreator-integrated Designer or non-integrated Designer.
    On the past documentations, I found a command - Q_EXPORT_PLUGIN2(), which is not in the customwidgetplugin source code example in 5.3. Is this actually required in this version?

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,013 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Properly adding custom widget into Qt Designer

    As I said you need to build the plugin so that it is binary compatible with your Qt Creator which has its own Qt binaries. You can check the version from Creator's help menu.

    Alternatively you can build Creator yourself from sources with the version of Qt you are using for your programs and then you will be able to use the plugin built with the same toolchain.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Jan 2015
    Posts
    22
    Thanks
    3
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Properly adding custom widget into Qt Designer

    Quote Originally Posted by wysota View Post
    As I said you need to build the plugin so that it is binary compatible with your Qt Creator which has its own Qt binaries. You can check the version from Creator's help menu.

    Alternatively you can build Creator yourself from sources with the version of Qt you are using for your programs and then you will be able to use the plugin built with the same toolchain.
    The about window says "Qt Creator 3.1.1, based on Qt 5.2.1 (MSVC 2010) 32 bit". So I have to either compile the plugin again to Qt 5.2.1 & MSVC 2010, or compile my own Creator with that version?

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,013 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Properly adding custom widget into Qt Designer

    Basically yes. It doesn't have to be Qt 5.2.1, it can be any of Qt 5.2.x, but it does have to be 32bit MSVC2010. If you decide to build Qt Creator yourself, you are free to choose any compiler and any supported Qt version.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. The following user says thank you to wysota for this useful post:

    Baltharos (20th January 2015)

  8. #7
    Join Date
    Jan 2015
    Posts
    22
    Thanks
    3
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Properly adding custom widget into Qt Designer

    Quote Originally Posted by wysota View Post
    Basically yes. It doesn't have to be Qt 5.2.1, it can be any of Qt 5.2.x, but it does have to be 32bit MSVC2010. If you decide to build Qt Creator yourself, you are free to choose any compiler and any supported Qt version.
    Okay, thanks a lot! Also a question again (I'm sorry): I assume that to embed my own widgets/plugins to QtCreator/Designer I have to refer to the Creator build version, but if I want to add custom libraries/plugins to my applications using Qt 5.3 compiled with mingw, I have to build them with version 5.3 + mingw right?

  9. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,013 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Properly adding custom widget into Qt Designer

    Quote Originally Posted by Baltharos View Post
    but if I want to add custom libraries/plugins to my applications using Qt 5.3 compiled with mingw, I have to build them with version 5.3 + mingw right?
    Yes, that's correct. Just bear in mind that if you split your plugin into two parts -- a library implementing the widget and a plugin itself that uses this library (be it statically or dynamically linked), you can link your application againt that library only.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  10. #9
    Join Date
    Jan 2015
    Posts
    22
    Thanks
    3
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Properly adding custom widget into Qt Designer

    Quote Originally Posted by wysota View Post
    Yes, that's correct. Just bear in mind that if you split your plugin into two parts -- a library implementing the widget and a plugin itself that uses this library (be it statically or dynamically linked), you can link your application againt that library only.
    Okay, thanks a lot! It works now, although I have to use version 5.2.1 and version 5.3.0 together. Is it possible to use 5.3.2-compiled library/plugins in 5.3.0-compiled applications? I'm gonna try to shift to online installer (so I can use a single Creator), and the one offered is 5.3.2..
    Last edited by Baltharos; 21st January 2015 at 07:39.

  11. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,013 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Properly adding custom widget into Qt Designer

    Quote Originally Posted by Baltharos View Post
    Is it possible to use 5.3.2-compiled library/plugins in 5.3.0-compiled applications?
    It should be ok.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  12. #11
    Join Date
    Jan 2015
    Posts
    22
    Thanks
    3
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Properly adding custom widget into Qt Designer

    Quote Originally Posted by wysota View Post
    It should be ok.
    Thanks a lot!

  13. #12
    Join Date
    Jan 2015
    Posts
    22
    Thanks
    3
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Properly adding custom widget into Qt Designer

    Sorry for bumping this thread, but I got another problem.. I managed to compile the dll, put it on the Designer, and it shows on the window form. But I can't access any of the custom methods, creating error "cannot locate ntablew.h". The custom widget subclass QTableWidget, and I use the template from Widget Collection.

    for ntablew.h
    Qt Code:
    1. #ifndef NTABLEW_H
    2. #define NTABLEW_H
    3.  
    4. #include <QTableWidget>
    5. #include <QStandardItemModel>
    6.  
    7. class NTableW : public QTableWidget
    8. {
    9. Q_OBJECT
    10.  
    11. public:
    12. NTableW(QWidget *parent = 0);
    13. void freezeRow(bool froze, int row);
    14. void unfreezeRow(int row);
    15.  
    16. public slots:
    17. void showFrozenRow();
    18. void hideFrozenRow();
    19.  
    20. protected:
    21. virtual void resizeEvent(QResizeEvent *event);
    22.  
    23. private:
    24. QTableView * m_frozentableview;
    25. QStandardItemModel * m_frozenmodel;
    26. bool m_frozen;
    27. int m_frozenrow;
    28. void init();
    29. void updateFrozenTableGeometry();
    30.  
    31. private slots:
    32. void updateSectionWidth(int logicalIndex, int oldSize, int newSize);
    33. void updateSectionHeight(int logicalIndex, int oldSize, int newSize);
    34. };
    35.  
    36. #endif
    To copy to clipboard, switch view to plain text mode 

    For ntablew.cpp
    Qt Code:
    1. #include "ntablew.h"
    2. #include <QHeaderView>
    3. #include "colordelegate.h"
    4.  
    5. NTableW::NTableW(QWidget *parent) :
    6. QTableWidget(parent)
    7. {
    8. m_frozen = false;
    9.  
    10. m_frozentableview = new QTableView();
    11. m_frozenmodel = new QStandardItemModel();
    12.  
    13. connect(horizontalHeader(),SIGNAL(sectionResized(int,int,int)), this,
    14. SLOT(updateSectionWidth(int,int,int)));
    15. connect(verticalHeader(),SIGNAL(sectionResized(int,int,int)), this,
    16. SLOT(updateSectionHeight(int,int,int)));
    17.  
    18. connect(m_frozentableview->horizontalScrollBar(), SIGNAL(valueChanged(int)),
    19. horizontalScrollBar(), SLOT(setValue(int)));
    20. connect(horizontalScrollBar(), SIGNAL(valueChanged(int)),
    21. m_frozentableview->horizontalScrollBar(), SLOT(setValue(int)));
    22.  
    23. }
    24.  
    25. void NTableW::freezeRow(bool froze, int row = 0)
    26. {
    27. m_frozen = froze;
    28. m_frozenrow = row;
    29. setItemDelegateForRow(m_frozenrow, new ColorDelegate(this));
    30. if(froze){
    31. init();
    32. }else{
    33. m_frozentableview->lower();
    34. }
    35. }
    36.  
    37. void NTableW::unfreezeRow(int row){
    38. m_frozentableview->lower();
    39. m_frozenmodel->clear();
    40. }
    41.  
    42. void NTableW::resizeEvent(QResizeEvent * event)
    43. {
    44. //QTableView::resizeEvent(event);
    45. updateFrozenTableGeometry();
    46. }
    47.  
    48. void NTableW::init()
    49. {
    50. QStandardItem * newItem = 0;
    51. for(int i = 0; i < columnCount(); i++){
    52. newItem = new QStandardItem(item(m_frozenrow, i)->text());
    53. m_frozenmodel->setItem(0, i, newItem);
    54. }
    55.  
    56. m_frozentableview->setModel(m_frozenmodel);
    57.  
    58. m_frozentableview->setFocusPolicy(Qt::NoFocus);
    59. m_frozentableview->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    60.  
    61. viewport()->stackUnder(m_frozentableview);
    62.  
    63. m_frozentableview->setSelectionModel(selectionModel());
    64. QHeaderView * horizontalHeaders = m_frozentableview->horizontalHeader();
    65. horizontalHeaders->setVisible(false);
    66. QHeaderView * verticalHeaders = m_frozentableview->verticalHeader();
    67. verticalHeaders->sectionResizeMode(QHeaderView::Fixed);
    68. if(!m_siderow) verticalHeaders->setVisible(false);
    69. m_frozentableview->setItemDelegateForRow(m_frozenrow, new ColorDelegate(m_frozentableview));
    70.  
    71. m_frozentableview->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    72. m_frozentableview->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    73. m_frozentableview->show();
    74.  
    75. updateFrozenTableGeometry();
    76.  
    77. setHorizontalScrollMode(ScrollPerPixel);
    78. setVerticalScrollMode(ScrollPerPixel);
    79. m_frozentableview->setVerticalScrollMode(ScrollPerPixel);
    80. }
    81.  
    82. void NTableW::showFrozenRow()
    83. {
    84. m_frozentableview->raise();
    85. }
    86.  
    87. void NTableW::hideFrozenRow()
    88. {
    89. m_frozentableview->lower();
    90. }
    91.  
    92. void NTableW::updateSectionWidth(int logicalIndex, int /* oldSize */, int newSize)
    93. {
    94. m_frozentableview->setColumnWidth(logicalIndex, newSize);
    95. }
    96.  
    97. void NTableW::updateSectionHeight(int logicalIndex, int /* oldSize */, int newSize)
    98. {
    99. if (logicalIndex == m_frozenrow){
    100. m_frozentableview->setRowHeight(m_frozenrow, newSize);
    101. }
    102. }
    103.  
    104. void NTableW::updateFrozenTableGeometry(){
    105. if(rowViewportPosition(m_frozenrow) <= horizontalHeader()->height()){
    106. m_frozentableview->setGeometry(verticalHeader()->width()+frameWidth(),
    107. horizontalHeader()->height(), viewport()->width(), rowHeight(m_frozenrow));
    108. }else if(rowViewportPosition(m_frozenrow) >= viewport()->height() - rowHeight(m_frozenrow) - horizontalHeader()->height()){
    109. m_frozentableview->setGeometry(verticalHeader()->width()+frameWidth(),
    110. viewport()->height()-rowHeight(m_frozenrow)-horizontalHeader()->height(), viewport()->width(), rowHeight(m_frozenrow));
    111. }else{
    112. m_frozentableview->setGeometry(verticalHeader()->width() + frameWidth(),
    113. rowViewportPosition(m_frozenrow), viewport()->width(), rowHeight(m_frozenrow));
    114. }
    115. }
    To copy to clipboard, switch view to plain text mode 

    For ntablewidgetplugin.h
    Qt Code:
    1. #ifndef NTABLEWPLUGIN_H
    2. #define NTABLEWPLUGIN_H
    3.  
    4. #include <QDesignerCustomWidgetInterface>
    5.  
    6. class NTableWPlugin : public QObject, public QDesignerCustomWidgetInterface
    7. {
    8. Q_OBJECT
    9. #if QT_VERSION >= 0x050000
    10. Q_PLUGIN_METADATA(IID "org.qt-project.Qt.QDesignerCustomWidgetInterface")
    11. #endif // QT_VERSION >= 0x050000
    12.  
    13. public:
    14. NTableWPlugin(QObject *parent = 0);
    15.  
    16. bool isContainer() const;
    17. bool isInitialized() const;
    18. QIcon icon() const;
    19. QString domXml() const;
    20. QString group() const;
    21. QString includeFile() const;
    22. QString name() const;
    23. QString toolTip() const;
    24. QString whatsThis() const;
    25. QWidget *createWidget(QWidget *parent);
    26. void initialize(QDesignerFormEditorInterface *core);
    27.  
    28. private:
    29. bool m_initialized;
    30. };
    31.  
    32. #endif
    To copy to clipboard, switch view to plain text mode 

  14. #13
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,013 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Properly adding custom widget into Qt Designer

    Where did you put ntablew.h?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  15. #14
    Join Date
    Jan 2015
    Posts
    22
    Thanks
    3
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Properly adding custom widget into Qt Designer

    The template put it in its own folder with ntablew.pri and ntablew.cpp. I just copied the resulting dll into plugins/designer folder, and the custom widget won't show methods in new projects. I didn't copy anything from the dll project onto the new project. When I tried to manually call the methods, that's when the error happens.

  16. #15
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,013 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Properly adding custom widget into Qt Designer

    If the project does not include ntablew.h then how do you expect to call methods defined in a class your compiler knows nothing about? If you call methods from QWidget then you include QWidget. The same is with your custom widget.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  17. #16
    Join Date
    Jan 2015
    Posts
    22
    Thanks
    3
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Properly adding custom widget into Qt Designer

    I've read some threads about this, but I didn't pay any heed back then..

    So even though I put the dll, the cpp and h file (or the pri file) have to be included in the project so the widget's methods can be called?

  18. #17
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,013 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Properly adding custom widget into Qt Designer

    Your widget is no different than any other library you link with -- you need the library and a header file which describes classes contained within it. Just like you need qwidget.h and QtWidgets to use QWidget class (you don't need qwidget.cpp, right?).
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  19. #18
    Join Date
    Jan 2015
    Posts
    22
    Thanks
    3
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Properly adding custom widget into Qt Designer

    I'm so sorry, but I still don't get it. The header file alone with the custom widget can call the methods (at least show them), but when I tries to run it the custom methods fail to compile (undefined reference). It only works when the cpp file is also included.

  20. #19
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,013 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Properly adding custom widget into Qt Designer

    Because you are not linking with your plugin.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  21. #20
    Join Date
    Jan 2015
    Posts
    22
    Thanks
    3
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Properly adding custom widget into Qt Designer

    So I need to.. Link the resulting library and header file into new projects? Changes the .pro file to include the folder of the dll project?

Similar Threads

  1. Replies: 7
    Last Post: 18th August 2011, 14:43
  2. Replies: 1
    Last Post: 23rd June 2011, 23:09
  3. Custom widget won't size properly in a table cell
    By zeroknowledge in forum Qt Programming
    Replies: 1
    Last Post: 11th May 2011, 16:32
  4. Adding Custom Widgets to the QT Designer
    By Polnareff in forum Qt Programming
    Replies: 1
    Last Post: 17th May 2010, 21:31
  5. Simple custom widget won't size properly
    By MrGarbage in forum Qt Tools
    Replies: 2
    Last Post: 9th August 2007, 13:12

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.