Results 1 to 2 of 2

Thread: problem drawing image

  1. #1
    Join Date
    Feb 2012
    Location
    Armenia/Yerevan
    Posts
    400
    Thanks
    15
    Thanked 16 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default problem drawing image

    I have problems drawing the output image in this program. The attached zip file contains the svg formatted images for the output.
    Let's jump to my question:

    1. The top n bottom rectangles are images. I want the angles of the bottom rectangle look rounded. In lines 254 to 257, a rectangle is created using QRect, I need to change this so that the bottom angles of the bottom rectangle look rounded.

    2. You know, In lines 259 to 264 another left rectangle is to be added to the shape. How can I paint this rectangle to black when I create it?

    I would thank if you add the required code to the following:

    Qt Code:
    1. #include <QtCore/QDebug>
    2. #include <QtGui/QMouseEvent>
    3. #include <QtGui/QPainter>
    4. #include <QtSvg/QSvgRenderer>
    5.  
    6. #include "qtmultislider.h"
    7.  
    8. /*!
    9.  
    10.   \class QtMultiSlider qtmultislider.h
    11.  
    12.   The description of the QtMultiSlider.
    13.  
    14.   \brief The QtMultiSlider class provides a double slider with SVG
    15.   graphics.
    16.  
    17.   The QtMultiSlider is an example to show the capabilities of the Qt Framework
    18.   related to customized controls.
    19.  
    20.   Here is a possible implementation shown
    21.  
    22.   \code
    23.  
    24.   // Create a QtSvgDialGauge
    25.   QWidget * widget = new QWidget(this);
    26.   QtMultiSlider * slider = new QtMultiSlider("&Range", widget);
    27.   slider->setRange(0, 100);
    28.   slider->setMaximumRange(70);
    29.   slider->setValue(80);
    30.   slider->setSkin("Beryl");
    31.   widget->addWidget(slider);
    32.   widget->show();
    33.  
    34.   \endcode
    35.  
    36.   It is important to call the function \a setSkin() to load a skin. If \a setSkin() is not called
    37.   the QtMultiSlider will not have any visible content.
    38.   The parameter \p skin has the skin name. The skins are in the resource file defined in the project file.
    39.   The name scheme in the resource file is "Skin Name"/"unique picture name.svg".
    40.  
    41.   The \a setSkin() function loads the diffrent skins over the "Skin name".
    42.   The needed graphics for this control are "normal.svg", "hovered.svg" and "pressed.svg".
    43.   If you want to add new skins, they have to follow the name scheme to work.
    44.  
    45.   The current skin can be read over the \a skin() function.
    46.  
    47.   \sa skin(), setSkin()
    48.  
    49.  
    50.   Signals: \n
    51.  
    52.   \fn void maximumExceeded()
    53.   \fn void minimumExceeded()
    54.  
    55.   Slots: \n
    56.  
    57.   \fn void setMaximumRange()
    58.   \fn void setMinimumRange()
    59.   \fn void setValue()
    60.  
    61. */
    62.  
    63. /*! \class QtMultiSliderHelper qtmultiSlider.h
    64.   \brief Private class of QtMultiSlider
    65. */
    66.  
    67. QtMultiSliderHelper::QtMultiSliderHelper(QWidget * parent)
    68. : QAbstractSlider(parent)
    69. {
    70. }
    71.  
    72. QtMultiSliderHelper::~QtMultiSliderHelper()
    73. {
    74. }
    75.  
    76. void QtMultiSliderHelper::paintEvent(QPaintEvent * event)
    77. {
    78. Q_UNUSED(event)
    79. }
    80.  
    81. QtMultiSlider::QtMultiSlider(QWidget * parent)
    82. : QProgressBar(parent)
    83. {
    84. init();
    85. }
    86.  
    87. QtMultiSlider::~QtMultiSlider()
    88. {
    89. }
    90.  
    91. void QtMultiSlider::setSkin(const QString& skin)
    92. {
    93. m_skin = skin;
    94. const QString base = ":/multislider/" + skin + '/';
    95.  
    96. m_rendererValueBar->load(base + "valuebar.svg");
    97. m_rendererValueBarFilled->load(base + "valuebar_filled.svg");
    98. m_rendererValueBarTop->load(base + "valuebar_top.svg");
    99. m_rendererValueBarBottom->load(base + "valuebar_bottom.svg");
    100.  
    101. m_topSliderRenderer = m_rendererTopSlider;
    102. m_bottomSliderRenderer = m_rendererBottomSlider;
    103.  
    104. // update geometry for new sizeHint and repaint
    105. updateGeometry();
    106. update();
    107. }
    108.  
    109. QString QtMultiSlider::skin() const
    110. {
    111. return m_skin;
    112. }
    113.  
    114. void QtMultiSlider::init()
    115. {
    116.  
    117.  
    118. m_previousExceededMaximum = false;
    119. m_previousExceededMinimum = false;
    120.  
    121. m_rendererValueBar = new QtSvgPixmapCache(this);
    122. m_rendererValueBarFilled = new QtSvgPixmapCache(this);
    123. m_rendererValueBarTop = new QtSvgPixmapCache(this);
    124. m_rendererValueBarBottom = new QtSvgPixmapCache(this);
    125. m_rendererTopSlider = new QtSvgPixmapCache(this);
    126. m_rendererTopSliderHovered = new QtSvgPixmapCache(this);
    127. m_rendererTopSliderPressed = new QtSvgPixmapCache(this);
    128. m_rendererBottomSlider = new QtSvgPixmapCache(this);
    129. m_rendererBottomSliderHovered = new QtSvgPixmapCache(this);
    130. m_rendererBottomSliderPressed = new QtSvgPixmapCache(this);
    131. m_rendererGroove = new QtSvgPixmapCache(this);
    132.  
    133. m_topSliderRenderer = m_rendererTopSlider;
    134. m_bottomSliderRenderer = m_rendererBottomSlider;
    135.  
    136. //Prepare for mouse over ( "hover") detection
    137. setMouseTracking(true);
    138. setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
    139.  
    140. m_topSlider = new QtMultiSliderHelper(this);
    141. m_topSlider->setValue(maximum());
    142. m_topSlider->setMaximum(maximum());
    143. m_topSlider->setMinimum(minimum());
    144.  
    145. m_bottomSlider = new QtMultiSliderHelper(this);
    146. m_bottomSlider->setValue(minimum());
    147. m_bottomSlider->setMaximum(maximum());
    148. m_bottomSlider->setMinimum(minimum());
    149.  
    150. connect(m_topSlider, SIGNAL(valueChanged(int)), SLOT(checkMaximumRange(int)));
    151. connect(m_bottomSlider, SIGNAL(valueChanged(int)), SLOT(checkMinimumRange(int)));
    152. }
    153.  
    154.  
    155. QtMultiSliderHelper * QtMultiSlider::topSlider()
    156. {
    157. return m_topSlider;
    158. }
    159.  
    160.  
    161. QtMultiSliderHelper * QtMultiSlider::bottomSlider()
    162. {
    163. return m_bottomSlider;
    164. }
    165.  
    166.  
    167. void QtMultiSlider::setValue(int value)
    168. {
    169. QProgressBar::setValue(value);
    170.  
    171. checkMaximumRange(topSlider()->value());
    172. checkMinimumRange(bottomSlider()->value());
    173. }
    174.  
    175.  
    176. void QtMultiSlider::checkMaximumRange(int topSliderValue)
    177. {
    178. const bool exceeded = value() > topSliderValue;
    179. if (m_previousExceededMaximum != exceeded) {
    180. m_previousExceededMaximum = exceeded;
    181. emit maximumExceeded(exceeded);
    182. }
    183. }
    184.  
    185.  
    186. void QtMultiSlider::checkMinimumRange(int bottomSliderValue)
    187. {
    188. const bool exceeded = value() < bottomSliderValue;
    189. if (m_previousExceededMinimum != exceeded) {
    190. m_previousExceededMinimum = exceeded;
    191. emit minimumExceeded(exceeded);
    192. }
    193. }
    194.  
    195. void QtMultiSlider::setMaximumRange(int maximum)
    196. {
    197. topSlider()->setValue(maximum);
    198. }
    199.  
    200.  
    201. void QtMultiSlider::setMinimumRange(int minimum)
    202. {
    203. bottomSlider()->setValue(minimum);
    204. }
    205.  
    206.  
    207. void QtMultiSlider::paintEvent(QPaintEvent * event)
    208. {
    209. Q_UNUSED(event)
    210.  
    211. if (!isVisible())
    212. return;
    213.  
    214. QPainter painter;
    215. painter.begin(this);
    216.  
    217. // Calculating the layout:
    218. int spacing = 30;
    219. int labelHeight = 0;
    220.  
    221. int valueBarWidth = 300;
    222.  
    223. // Calculating the maximum string size
    224.  
    225. int valueBarX = spacing;
    226.  
    227. int valueBarTopY = 0;
    228. int valueBarBottomY = 0;
    229.  
    230. QSizeF originalSize = m_rendererValueBarTop->defaultSize();
    231. QSizeF targetSize = originalSize;
    232. targetSize.scale(QSizeF(valueBarWidth, originalSize.height()), Qt::KeepAspectRatio);
    233. qreal scaleRatio = targetSize.width() /
    234. originalSize.width();
    235.  
    236. QSize valueBarTopSize = m_rendererValueBarTop->defaultSize() * scaleRatio;
    237.  
    238.  
    239. QRect valueBarTopRect = QRect(QPoint(valueBarX, valueBarTopY),
    240. QPoint(valueBarX + 300, valueBarTopSize.height()));
    241.  
    242. m_rendererValueBarTop->render(&painter, valueBarTopRect);
    243.  
    244. QSize valueBarBottomSize = m_rendererValueBarBottom->defaultSize() * scaleRatio;
    245.  
    246. valueBarBottomY = height() - labelHeight - valueBarTopRect.bottom()
    247. - valueBarBottomSize.height();
    248.  
    249. m_valueBarRightRect = QRect(QPoint(valueBarX, valueBarTopRect.bottom()),
    250. QSize(valueBarWidth, valueBarBottomY));
    251.  
    252. m_rendererValueBar->render(&painter, m_valueBarRightRect);
    253.  
    254. QRect valueBarBottomRect = QRect(QPoint(valueBarX, m_valueBarRightRect.bottom()),
    255. QPoint(valueBarX + 300, 200));
    256.  
    257. m_rendererValueBarBottom->render(&painter, valueBarBottomRect);
    258.  
    259. /*
    260.   m_valueBarLeftRect = QRect(QPoint(valueBarX - 20, valueBarTopRect.top() ),
    261.   QSize(valueBarX - 10, valueBarBottomRect.bottom() ));
    262.  
    263.   m_rendererValueBar->render(&painter, m_valueBarLeftRect);
    264. */
    265.  
    266. // Drawing the actual value bar
    267. int filledPixels = ((double)value()) / maximum() * m_valueBarRightRect.height();
    268. QRect filledRect = m_valueBarRightRect;
    269. filledRect.setTop(filledRect.bottom() - filledPixels);
    270. m_rendererValueBarFilled->render(&painter, filledRect);
    271.  
    272. painter.end();
    273.  
    274. }
    275.  
    276. //----------------------------------------------------
    277.  
    278.  
    279. int QtMultiSlider::valueToPixel(int value)
    280. {
    281. return (double)(value) / (double)(maximum() - minimum())
    282. * (double)(m_valueBarRightRect.bottom() - m_valueBarRightRect.top());
    283. }
    284.  
    285. int QtMultiSlider::pixelToValue(int pixel)
    286. {
    287. if (pixel < 0)
    288. return minimum();
    289. if (pixel > m_valueBarRightRect.bottom())
    290. return maximum();
    291.  
    292. return (double)(pixel) / (double)(m_valueBarRightRect.bottom() - m_valueBarRightRect.top())
    293. * (double)(maximum() - minimum());
    294. }
    295.  
    296.  
    297. QSize QtMultiSlider::minimumSizeHint() const
    298. {
    299. return QSize(80, 100);
    300. }
    301.  
    302. QSize QtMultiSlider::sizeHint() const
    303. {
    304. return QSize(100, 200);
    305. }
    306.  
    307. void QtMultiSlider::leaveEvent(QEvent * event)
    308. {
    309. Q_UNUSED(event)
    310.  
    311. update();
    312. }
    To copy to clipboard, switch view to plain text mode 
    Attached Images Attached Images
    Attached Files Attached Files
    Last edited by saman_artorious; 24th February 2012 at 17:31. Reason: add an image

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: problem drawing image

    I am not sure why this has anything to do with SVG.

    Normally if you want a rounded rectangle when rendering your widget you would use QPainter::drawRoundedRect(). If you want a filled rectangle then use QPainter::fillRect().

Similar Threads

  1. Drawing a rectangle over Qlabel image
    By cheyanne in forum Newbie
    Replies: 16
    Last Post: 13th October 2011, 09:05
  2. QGraphicsscene Image drawing
    By augusbas in forum Qt Programming
    Replies: 1
    Last Post: 4th November 2010, 09:38
  3. Drawing over an image displayed on a QLabel
    By franco.amato in forum Qt Programming
    Replies: 10
    Last Post: 25th August 2010, 05:40
  4. problem in drawing image in QLabel!
    By mismael85 in forum Qt Programming
    Replies: 2
    Last Post: 28th March 2010, 15:16
  5. Fast image drawing/scaling in Qt 3.3
    By eriwik in forum Qt Programming
    Replies: 1
    Last Post: 21st June 2006, 10:45

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
  •  
Qt is a trademark of The Qt Company.