Results 1 to 14 of 14

Thread: Proper code

  1. #1
    Join Date
    Mar 2010
    Location
    Auckland, NZ
    Posts
    121
    Thanks
    9
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Proper code

    I have a limited understanding of Qt and Qwt, as this post will reveal, and I apologise in advance for my ignorance.
    Despite my poor grasp of it all, I have managed to get QwtPlots working in some fairly complex Qt programs. Now I am trying to do something much simpler, but following (so I think) the procedure that I have working in the other programs. But this simple example doesn't work.

    I have attached the three files that make up the project, which I am building with Qt Creator 2.5.2 on VS 2012 with Qt 4.8.5 (64-bit). Qwt 6.1.3 is also built 64-bit. The missing component is mainwindow.ui, which I made by hand. It has a QMainWindow containing a QWidget (centralWidget), within which I placed a QVBoxLayout, and inside that a QMdiArea called mdiArea.

    The program compiles and runs, but nothing I do makes it display the QwtPlot. mdiArea just shows as a grey rectangle. As far as I can see I am following my usual procedure, except that in the other programs centralWidget contains a QStackedWidget, and within that the vertical layout that holds mdiArea. I know that I am using an approach different from anything I've seen in the Qwt examples, but it seems to me that it should be close to working.

    If somebody has the patience to look at my code and see where I am going wrong I would be very grateful.
    Attached Files Attached Files

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,229
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Proper code

    I don't see anywhere in your MainWindow constructor where you actually create the QwtPlot instance. You declare a pointer to one as a local variable, don't initialize it, and then proceed to use it. I am surprised the code runs at all.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. #3
    Join Date
    Mar 2010
    Location
    Auckland, NZ
    Posts
    121
    Thanks
    9
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Proper code

    In the night (!) I realised that I had a stupid mistake in my code - I needed pGraph to be the object, not the pointer. I modified the code (see attached mainwindow.cpp) but the result is the same - I see just a grey rectangle. (By the way, I had previously tried the "fixed" code, without success, and without thinking much tried the dumb pointer version.)
    I also don't understand why the code ran at all! Uwe will know. But is there a way to make this approach work in this simple example? In my other programs I set up 20 QwtPlots in the mdiArea, the only difference that's obvious to me is that now I am not using the QStackedWidget.
    Attached Files Attached Files
    Last edited by gib; 6th July 2021 at 00:14.

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,229
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Proper code

    In the night (!) I realised that I had a stupid mistake in my code - I needed pGraph to be the object, not the pointer.
    You just replaced one mistake with another. The QwtPlot instance that you create on the stack in the MainWindow constructor is destroyed as soon as the constructor exits, so this code does effectively nothing.

    Go back to your original code and change it to this:

    Qt Code:
    1. MainWindow::MainWindow(QWidget *parent) :
    2. QMainWindow(parent),
    3. ui(new Ui::MainWindow)
    4. {
    5. ui->setupUi(this);
    6. QwtPlot *pGraph = new QwtPlot();
    7. pGraph->setTitle("Test Plot");
    8. pGraph->setCanvasBackground(Qt::white);
    9. pGraph->setAxisScale(QwtPlot::xBottom,0,100,0);
    10. ui->mdiArea->addSubWindow(pGraph);
    11. }
    To copy to clipboard, switch view to plain text mode 

    There is no need to call show() on anything. The MDI area and all of the windows in it will be shown as soon as the call to MainWindow::show() in main() occurs and the event loop starts.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  5. #5
    Join Date
    Mar 2010
    Location
    Auckland, NZ
    Posts
    121
    Thanks
    9
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Proper code

    Thanks. I made the changes, but the result is unchanged. In fact I think I did try this yesterday.
    I suspect the remaining error is in the way the ui is constructed. Do you think mdiArea needs to be "packaged" in any way? Currently it is just added to MainWindow's centralWidget.
    By the way I am now testing with 32-bit Qt and Qwt (as all my previous projects used.)

  6. #6
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,229
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Proper code

    Currently it is just added to MainWindow's centralWidget.
    What do you mean added? Are you calling QMainWindow::setCentralWidget() anywhere? Or are you calling it more than once with multiple widget instances?

    Have you "previewed" the MainWindow UI in Qt Designer? My guess is that you actually haven't added the QMdiArea properly in Qt Designer and it is not actually being set as the central widget. But you haven't posted the UI file so there is no way to check that code.

    Have you tried adding a subwindow with a different widget? Like a QTreeView or QPushButton or something else? (Because they have a minimum size). If that works, then you may have to give the QwtPlot a size before the sub window can be shown properly.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  7. #7
    Join Date
    Mar 2010
    Location
    Auckland, NZ
    Posts
    121
    Thanks
    9
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Proper code

    I'm sure I'm not using the correct terminology. I will explain how mainwindow.ui was created.
    For testing, I started with another simple Qt program, one that doesn't use Qwt (this program works fine.) In Qt Designer I clicked-and-dragged a QMdiArea widget onto the central widget. This is why I said I "added" it to the central widget. I do everything in Designer, and do not call setCentralWidget(). I will attach mainwindow.ui. You will see that it contains several things that are unrelated to what I am trying to do - these are from the other program. You can ignore everything except mdiArea.
    Thanks again for trying to help me! Unfortunately I am not a good C++ programmer, although I usually get things to work eventually.
    Attached Files Attached Files

  8. #8
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,229
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Proper code

    OK, I see now what you are trying to do. The usual way of using QMdiArea is to make it -the centralWidget- in a QMainWindow. You have a whole pile of widgets in the centralWidget - pushbuttons, labels, text edit, radio buttons, plus a QMdiArea.

    I believe your problem is that you are not using any layouts for your widgets. The documentation says:

    QMdiArea is commonly used as the center widget in a QMainWindow to create MDI applications, but can also be placed in any layout.
    The key word is "layout". You aren't using any layouts, you are simply placing widgets on screen and fixing their positions by specifying their geometry.

    Here is your UI with the central widget laid out with a QGridLayout. See if that gives you what you want when you run it. The point is that with a layout, the layout manages the positions and sizes of widgets inside it, so that as you change the size of your MainWindow at run time, things move nicely and behave properly.

    Hmmm, The forum doesn't seem to let me add an attachment to this reply. I'll save it, edit it, and see if that works. Nope. I'll attach it as code.

    Qt Code:
    1. <?xml version="1.0" encoding="UTF-8"?>
    2. <ui version="4.0">
    3. <class>MainWindow</class>
    4. <widget class="QMainWindow" name="MainWindow">
    5. <property name="geometry">
    6. <rect>
    7. <x>0</x>
    8. <y>0</y>
    9. <width>810</width>
    10. <height>673</height>
    11. </rect>
    12. </property>
    13. <property name="windowTitle">
    14. <string>Compressing tiff file</string>
    15. </property>
    16. <widget class="QWidget" name="centralWidget">
    17. <layout class="QGridLayout" name="gridLayout_2">
    18. <item row="0" column="0" rowspan="4" colspan="3">
    19. <layout class="QGridLayout" name="gridLayout">
    20. <item row="3" column="2">
    21. <widget class="QRadioButton" name="radioButtonZip">
    22. <property name="text">
    23. <string>Zip/Deflate</string>
    24. </property>
    25. <attribute name="buttonGroup">
    26. <string notr="true">buttonGroupMethod</string>
    27. </attribute>
    28. </widget>
    29. </item>
    30. <item row="4" column="2">
    31. <widget class="QPushButton" name="pushButtonCompress">
    32. <property name="text">
    33. <string>Compress</string>
    34. </property>
    35. </widget>
    36. </item>
    37. <item row="0" column="2">
    38. <widget class="QLabel" name="label_method">
    39. <property name="text">
    40. <string>Compression method</string>
    41. </property>
    42. </widget>
    43. </item>
    44. <item row="2" column="2">
    45. <widget class="QRadioButton" name="radioButtonLZW">
    46. <property name="text">
    47. <string>LZW</string>
    48. </property>
    49. <attribute name="buttonGroup">
    50. <string notr="true">buttonGroupMethod</string>
    51. </attribute>
    52. </widget>
    53. </item>
    54. <item row="0" column="1">
    55. <widget class="QLabel" name="label_2">
    56. <property name="minimumSize">
    57. <size>
    58. <width>150</width>
    59. <height>0</height>
    60. </size>
    61. </property>
    62. <property name="text">
    63. <string/>
    64. </property>
    65. </widget>
    66. </item>
    67. <item row="0" column="0">
    68. <widget class="QPushButton" name="pushButtonInputFile">
    69. <property name="maximumSize">
    70. <size>
    71. <width>150</width>
    72. <height>16777215</height>
    73. </size>
    74. </property>
    75. <property name="text">
    76. <string>Input image file</string>
    77. </property>
    78. </widget>
    79. </item>
    80. <item row="5" column="0">
    81. <widget class="QLabel" name="label">
    82. <property name="text">
    83. <string>Result:</string>
    84. </property>
    85. </widget>
    86. </item>
    87. <item row="1" column="0">
    88. <widget class="QPushButton" name="pushButtonOutputFile">
    89. <property name="text">
    90. <string>Output image file</string>
    91. </property>
    92. </widget>
    93. </item>
    94. <item row="1" column="2">
    95. <widget class="QRadioButton" name="radioButtonPackbits">
    96. <property name="text">
    97. <string>Packbits</string>
    98. </property>
    99. <property name="checked">
    100. <bool>true</bool>
    101. </property>
    102. <attribute name="buttonGroup">
    103. <string notr="true">buttonGroupMethod</string>
    104. </attribute>
    105. </widget>
    106. </item>
    107. <item row="0" column="3" rowspan="5">
    108. <widget class="QTextEdit" name="textEdit"/>
    109. </item>
    110. <item row="5" column="1" colspan="3">
    111. <widget class="QMdiArea" name="mdiArea">
    112. <property name="sizePolicy">
    113. <sizepolicy hsizetype="Expanding" vsizetype="Expanding">
    114. <horstretch>0</horstretch>
    115. <verstretch>0</verstretch>
    116. </sizepolicy>
    117. </property>
    118. <property name="maximumSize">
    119. <size>
    120. <width>1000</width>
    121. <height>16777215</height>
    122. </size>
    123. </property>
    124. </widget>
    125. </item>
    126. </layout>
    127. </item>
    128. <item row="1" column="1" colspan="2">
    129. <widget class="QLabel" name="labelInputFile">
    130. <property name="text">
    131. <string/>
    132. </property>
    133. </widget>
    134. </item>
    135. <item row="2" column="1" colspan="2">
    136. <widget class="QLabel" name="labelOutputFile">
    137. <property name="text">
    138. <string/>
    139. </property>
    140. </widget>
    141. </item>
    142. <item row="3" column="2">
    143. <widget class="QFrame" name="frame_method">
    144. <property name="frameShape">
    145. <enum>QFrame::StyledPanel</enum>
    146. </property>
    147. <property name="frameShadow">
    148. <enum>QFrame::Raised</enum>
    149. </property>
    150. </widget>
    151. </item>
    152. <item row="4" column="0" colspan="3">
    153. <widget class="QLabel" name="labelResult">
    154. <property name="text">
    155. <string/>
    156. </property>
    157. </widget>
    158. </item>
    159. </layout>
    160. </widget>
    161. <widget class="QMenuBar" name="menuBar">
    162. <property name="geometry">
    163. <rect>
    164. <x>0</x>
    165. <y>0</y>
    166. <width>810</width>
    167. <height>23</height>
    168. </rect>
    169. </property>
    170. </widget>
    171. <widget class="QToolBar" name="mainToolBar">
    172. <attribute name="toolBarArea">
    173. <enum>TopToolBarArea</enum>
    174. </attribute>
    175. <attribute name="toolBarBreak">
    176. <bool>false</bool>
    177. </attribute>
    178. </widget>
    179. <widget class="QStatusBar" name="statusBar"/>
    180. </widget>
    181. <layoutdefault spacing="6" margin="11"/>
    182. <resources/>
    183. <connections>
    184. <connection>
    185. <sender>pushButtonOutputFile</sender>
    186. <signal>clicked()</signal>
    187. <receiver>MainWindow</receiver>
    188. <slot>outputFileSelecter()</slot>
    189. <hints>
    190. <hint type="sourcelabel">
    191. <x>31</x>
    192. <y>84</y>
    193. </hint>
    194. <hint type="destinationlabel">
    195. <x>56</x>
    196. <y>207</y>
    197. </hint>
    198. </hints>
    199. </connection>
    200. <connection>
    201. <sender>pushButtonInputFile</sender>
    202. <signal>clicked()</signal>
    203. <receiver>MainWindow</receiver>
    204. <slot>inputFileSelecter()</slot>
    205. <hints>
    206. <hint type="sourcelabel">
    207. <x>62</x>
    208. <y>43</y>
    209. </hint>
    210. <hint type="destinationlabel">
    211. <x>262</x>
    212. <y>59</y>
    213. </hint>
    214. </hints>
    215. </connection>
    216. <connection>
    217. <sender>pushButtonCompress</sender>
    218. <signal>clicked()</signal>
    219. <receiver>MainWindow</receiver>
    220. <slot>compresser()</slot>
    221. <hints>
    222. <hint type="sourcelabel">
    223. <x>309</x>
    224. <y>200</y>
    225. </hint>
    226. <hint type="destinationlabel">
    227. <x>420</x>
    228. <y>249</y>
    229. </hint>
    230. </hints>
    231. </connection>
    232. </connections>
    233. <slots>
    234. <slot>outputFileSelecter()</slot>
    235. <slot>inputFileSelecter()</slot>
    236. <slot>compresser()</slot>
    237. </slots>
    238. <buttongroups>
    239. <buttongroup name="buttonGroupMethod"/>
    240. </buttongroups>
    241. </ui>
    To copy to clipboard, switch view to plain text mode 
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  9. #9
    Join Date
    Mar 2010
    Location
    Auckland, NZ
    Posts
    121
    Thanks
    9
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Proper code

    Thanks again, d_stranz. Sadly, it doesn't help. In fact I had done some testing with a layout.
    Capture.jpg

  10. #10
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,229
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Proper code

    Then there must be something wrong with the way you are creating and using the QwtPlot instance. If I use this code for MainWindow.cpp (I deleted the "run()" slot):

    Qt Code:
    1. #include "MainWindow.h"
    2. #include "ui_MainWindow.h"
    3.  
    4. #include <QTreeView>
    5. #include <QPushButton>
    6.  
    7. MainWindow::MainWindow(QWidget *parent)
    8. : QMainWindow(parent)
    9. , ui(new Ui::MainWindow)
    10. {
    11. ui->setupUi(this);
    12. QTreeView * pTree= new QTreeView();
    13. ui->mdiArea->addSubWindow(pTree);
    14.  
    15. QPushButton * pButton = new QPushButton( "Push Me!");
    16. ui->mdiArea->addSubWindow(pButton);
    17. }
    18.  
    19. MainWindow::~MainWindow()
    20. {
    21. delete ui;
    22. }
    To copy to clipboard, switch view to plain text mode 

    and the MainWindow.ui file I posted yesterday, I get this:

    Capture.jpg

    All I have done is to substitute QTreeView and QPushButton instances as the contents of the two MDI subwindows.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  11. The following user says thank you to d_stranz for this useful post:

    gib (8th July 2021)

  12. #11
    Join Date
    Mar 2010
    Location
    Auckland, NZ
    Posts
    121
    Thanks
    9
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Proper code

    I'm sure you are right, d_stranz. Since this is the Qwt forum, I'm hoping that some Qwt guru can point out my error. As far as I can determine I am using QwtPlot in the essentially same way that I do in several other programs, but obviously something is different. The other programs all use the same procedure, in code much more complex than my test code. In those programs I select the plots to display from a list of candidates, and maintain an array of pointers to Plot objects, where the Plot object is as specified in the attachment. In my test code I am trying to use QwtPlot directly. I did try working with the Plot object, but with no better success.
    Attached Files Attached Files

  13. #12
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,229
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Proper code

    A QwtPlot is just another QWidget. If the QWidget is created properly and has a non-zero minimum size it should be properly displayed in a sub-window. This is why I chose QTreeView and QPushButton for the test case. For example, if I add these lines to the MainWindow constructor:

    Qt Code:
    1. QWidget * pWidget = new QWidget();
    2. ui->mdiArea->addSubWindow( pWidget );
    To copy to clipboard, switch view to plain text mode 

    all I get for that sub-window is the title bar and nothing else, because by default an empty QWidget has no size.

    Have you debugged your code and stepped through the MainWindow constructor to determine that the QwtPlot instance you create is actually valid (i.e. not a null pointer)? If I change the code above to this:

    Qt Code:
    1. QWidget * pWidget = nullptr;
    2. ui->mdiArea->addSubWindow( pWidget );
    To copy to clipboard, switch view to plain text mode 

    then nothing appears, not even the title bar.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  14. #13
    Join Date
    Mar 2010
    Location
    Auckland, NZ
    Posts
    121
    Thanks
    9
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Proper code

    I found my mistake yesterday, and thought I'd sent a reply to you. This site can be confusing - my reply didn't go. I'm still having problems: "Your submission could not be processed because the token has expired" Reloading the window doesn't help.
    The error was embarrassingly simple, and unrelated to QwtPlot and QMdiArea. Because the infoFile was not found, file.open() returned false, and the program returned without reaching the QwtPlot code. My stupid oversight. Thanks to your simple code example, which omitted that leftover code about the info file, I was able to see my mistake - finally. Please accept my thanks - and my apologies.

  15. #14
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,229
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Proper code

    Glad you got it to work. I was pretty sure it was something unrelated to QwtPlot unless it was an error in creating it (which in your case meant never creating it at all).
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. Proper way to start learning Qt
    By starlays in forum Newbie
    Replies: 2
    Last Post: 7th September 2016, 14:44
  2. Proper way to use qmake and moc?
    By mossen in forum Newbie
    Replies: 1
    Last Post: 6th May 2011, 03:52
  3. DOM XML reader - Not getting any proper output
    By mecrazycoder in forum Newbie
    Replies: 0
    Last Post: 17th December 2010, 15:26
  4. proper use of QList?
    By jhowland in forum Qt Programming
    Replies: 3
    Last Post: 13th September 2010, 15:57
  5. Proper way to #include Qt?
    By pherthyl in forum Qt Programming
    Replies: 1
    Last Post: 11th August 2006, 03:15

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.