[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-panel-devel
Subject:    Re: How to adjust applet size according to its child QGraphicsWidget?
From:       Dong Tiger <idlecat511 () gmail ! com>
Date:       2009-02-25 10:05:52
Message-ID: cba868840902250205r2376a686y19447087b06017ad () mail ! gmail ! com
[Download RAW message or body]

[Attachment #2 (multipart/alternative)]


2008/12/30 Alexis Ménard <menard@kde.org>

> Unfortunately you can't it is a bug in Qt.
>
> You can take a look to the task tracker of Qt Number 231114 and 211500. I
> guess it is what you try to solve. The fix is in 4.4 branch (so scheduled
> for 4.4.4) and in 4.5.


I still can't get this work even with latest Qt4.5. I've attached code of a
sample plasmoid which demonstrate the problem. You are welcomed to try it
out. Right click on the applet to modify its child widget size.


>
>
> As a workaround you have to manage the resize of your applet by hand when
> the size of the layout change.
>
The question is how can I do it? When the embedded QGraphicsWidget need to
enlarge, say from 200x200 to 400x400, how can I decide what the applet size
should be?


>
> 2008/12/30 Dong Tiger <idlecat511@gmail.com>
>
>> Hi,
>>
>> A QGraphicsWidget is embeded into Plasma::Applet through
>> QGraphicsLinearLayout. When this QGraphicsWidget's size changes, how to get
>> the applet's size adjusted accordingly?
>>
>> The code snippet looks like:
>>
>>   child = new MyGraphicsWidget(applet);
>>   layout = new QGraphicsLinearLayout(applet);
>>   layout->setSpacing(0);
>>   layout->addItem(child);
>>   applet->setLayout(layout);
>>
>>
>> Regards,
>> --
>> Tiger
>>
>> _______________________________________________
>> Plasma-devel mailing list
>> Plasma-devel@kde.org
>> https://mail.kde.org/mailman/listinfo/plasma-devel
>>
>>
>
> _______________________________________________
> Plasma-devel mailing list
> Plasma-devel@kde.org
> https://mail.kde.org/mailman/listinfo/plasma-devel
>
>

[Attachment #5 (text/html)]

<br><div class="gmail_quote">2008/12/30 Alexis Ménard <span dir="ltr">&lt;<a \
href="mailto:menard@kde.org">menard@kde.org</a>&gt;</span><br><blockquote \
class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt \
0pt 0.8ex; padding-left: 1ex;"> Unfortunately you can&#39;t it is a bug in \
Qt.<br><br>You can take a look to the task tracker of Qt Number 231114 and 211500. I \
guess it is what you try to solve. The fix is in 4.4 branch (so scheduled for 4.4.4) \
and in 4.5. </blockquote> <div><br>I still can&#39;t get this work even with latest \
Qt4.5. I&#39;ve attached code of a sample plasmoid which demonstrate the problem. You \
are welcomed to try it out. Right click on the applet to modify its child widget \
size.<br>  <br></div><blockquote class="gmail_quote" style="border-left: 1px solid \
rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;"><br> <br>As a \
workaround you have to manage the resize of your applet by hand when the size of the \
layout change.<br><div class="gmail_quote"></div></blockquote><div>The question is \
how can I do it? When the embedded QGraphicsWidget need to enlarge, say from 200x200 \
to 400x400, how can I decide what the applet size should be?<br>  \
<br></div><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, \
204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;"><div \
class="gmail_quote"><br>2008/12/30 Dong Tiger <span dir="ltr">&lt;<a \
href="mailto:idlecat511@gmail.com" \
target="_blank">idlecat511@gmail.com</a>&gt;</span><br>

<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); \
margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;"><div><div></div><div \
class="Wj3C7c">Hi,<br><br>A QGraphicsWidget is embeded into Plasma::Applet through \
QGraphicsLinearLayout. When this QGraphicsWidget&#39;s size changes, how to get the \
applet&#39;s size adjusted accordingly? <br>

<br>The code snippet looks like:<br>
<br>  child = new MyGraphicsWidget(applet);<br>  layout = new \
QGraphicsLinearLayout(applet);<br>  layout-&gt;setSpacing(0);<br>  \
layout-&gt;addItem(child);<br>  \
applet-&gt;setLayout(layout);<br><br><br>Regards,<br>--<br>

<font color="#888888">Tiger<br>
</font><br></div></div>_______________________________________________<br>
Plasma-devel mailing list<br>
<a href="mailto:Plasma-devel@kde.org" target="_blank">Plasma-devel@kde.org</a><br>
<a href="https://mail.kde.org/mailman/listinfo/plasma-devel" \
target="_blank">https://mail.kde.org/mailman/listinfo/plasma-devel</a><br> \
<br></blockquote></div><br> <br>_______________________________________________<br>
Plasma-devel mailing list<br>
<a href="mailto:Plasma-devel@kde.org">Plasma-devel@kde.org</a><br>
<a href="https://mail.kde.org/mailman/listinfo/plasma-devel" \
target="_blank">https://mail.kde.org/mailman/listinfo/plasma-devel</a><br> \
<br></blockquote></div><br>

--001636e1fd82820e4b0463bb616f--


["CMakeLists.txt" (text/plain)]

# Project Needs a name ofcourse
project(plasma-tutorial1)
 
# Find the required Libaries
find_package(Qt4 REQUIRED)
find_package(KDE4 REQUIRED)
include(KDE4Defaults)
 
add_definitions (${QT_DEFINITIONS} ${KDE4_DEFINITIONS})
include_directories(
   ${CMAKE_SOURCE_DIR}
   ${CMAKE_BINARY_DIR}
   ${KDE4_INCLUDES}
   ${Qt4_INCLUDES}}
   )
 
# We add our source code here
set(tutorial1_SRCS plasma-tutorial1.cpp)
 
# Now make sure all files get to the right place
kde4_add_plugin(plasma_applet_tutorial1 ${tutorial1_SRCS})
target_link_libraries(plasma_applet_tutorial1 
                      ${KDE4_PLASMA_LIBS} ${KDE4_KDEUI_LIBS} ${Qt4_LIBS})
 
install(TARGETS plasma_applet_tutorial1
        DESTINATION ${PLUGIN_INSTALL_DIR})
 
install(FILES plasma-applet-tutorial1.desktop
        DESTINATION ${SERVICES_INSTALL_DIR})

["plasma-tutorial1.cpp" (text/x-c++src)]

#include "plasma-tutorial1.h"
#include <QPainter>
#include <QFontMetrics>
#include <QSizeF>

#include <QPainter>
#include <QApplication>
#include <QInputDialog>
QSizePolicy::Policy policy = QSizePolicy::Preferred;

class MyGraphicsWidget: public QGraphicsWidget {
 public:
  MyGraphicsWidget(QGraphicsItem * parent = 0) : QGraphicsWidget(parent) {}
  void paint(QPainter *p, const QStyleOptionGraphicsItem *option, QWidget *widget = 0) {
    QRectF r = rect();
    p->setPen(QColor(0, 0, 255));
    p->drawLine(r.left(), r.top(), r.right(), r.bottom());
    p->drawLine(r.left(), r.bottom(), r.right(), r.top());
    p->drawRect(r);
    QSizeF s = parentWidget()->size();
    p->drawText(10, 10, QString("Child:%1x%2").arg(size().width()).arg(size().height()));
    p->drawText(10, 30, QString("Parent:%1x%2").arg(s.width()).arg(s.height()));
  }
};

static void embedWidget(QGraphicsWidget *parent, QWidget *widget) {
}

PlasmaTutorial1::PlasmaTutorial1(QObject *parent, const QVariantList &args)
    : Plasma::Applet(parent, args), layout_(NULL), child_(NULL)
{
    resize(200, 200);
    child_ = new MyGraphicsWidget(this);
  layout_ = new QGraphicsLinearLayout(this);
  layout_->setSpacing(0);
  layout_->addItem(child_);
  layout_->setSizePolicy(policy, policy);
  setLayout(layout_);

  setBackgroundHints(Plasma::Applet::NoBackground);
}


PlasmaTutorial1::~PlasmaTutorial1()
{
    if (hasFailedToLaunch()) {
        // Do some cleanup here
    } else {
        // Save settings
    }
}

void PlasmaTutorial1::init()
{
}

void PlasmaTutorial1::paintInterface(QPainter *p,
        const QStyleOptionGraphicsItem *option, const QRect &contentsRect)
{
}

void PlasmaTutorial1::constraintsEvent(Plasma::Constraints constraints) {
  kDebug() << "constraintsEvent";
}
void PlasmaTutorial1::contextMenuEvent(QGraphicsSceneContextMenuEvent * event) {
  QSize s = child_->size().toSize();

  int size = QInputDialog::getInteger(NULL, "Set Size", "Set size",
                                      s.width()*1000 + s.height());
  if (size == s.width()*1000 + s.height()) return;
  child_->resize(size/1000, size%1000);
  child_->setPreferredSize(size/1000, size%1000);
  // child_->setMinimumSize(size/1000, size%1000);
  setPreferredSize(layout_->preferredSize());
  updateGeometry();
  kDebug() << "mouseDoubleClickEvent";
}
#include "plasma-tutorial1.moc"

["plasma-tutorial1.h" (text/x-chdr)]

#ifndef Tutorial1_HEADER
#define Tutorial1_HEADER
// We need the Plasma Applet headers
#include <KIcon>

#include <Plasma/Applet>
#include <Plasma/Svg>

#include <QGraphicsLinearLayout>

class QSizeF;

// Define our plasma Applet
class PlasmaTutorial1 : public Plasma::Applet
{
    Q_OBJECT
    public:
        // Basic Create/Destroy
        PlasmaTutorial1(QObject *parent, const QVariantList &args);
        ~PlasmaTutorial1();

        // The paintInterface procedure paints the applet to screen
        void paintInterface(QPainter *painter,
                const QStyleOptionGraphicsItem *option,
                const QRect& contentsRect);
        void init();
    protected:
        virtual void constraintsEvent(Plasma::Constraints constraints);
        virtual void contextMenuEvent(QGraphicsSceneContextMenuEvent * event);
        QGraphicsLinearLayout *layout_;
        QGraphicsWidget* child_;
};

// This is the command that links your applet to the .desktop file
K_EXPORT_PLASMA_APPLET(tutorial1, PlasmaTutorial1)
#endif

["plasma-applet-tutorial1.desktop" (application/octet-stream)]

_______________________________________________
Plasma-devel mailing list
Plasma-devel@kde.org
https://mail.kde.org/mailman/listinfo/plasma-devel


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic