|
|
Source code |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
qmlRegisterType<CustomCanvasClass>("CustomCanvasClass", 1, 0, "CustomCanvasClass");
QDeclarativeView *viewer = new QDeclarativeView;
viewer->setSource(QUrl::fromLocalFile("qml/sample/main.qml"));
viewer->setFixedSize(480,272);
viewer->show();
return app.exec();
}
|
|
|
Source code |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
void CustomCanvasClass::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
{
int lineThickness = (m_outerDia - m_innerDia)/2;
QPen pen(m_color, lineThickness);
pen.setStyle(Qt::SolidLine);
QBrush brush(Qt::transparent);
painter->setPen(pen);
painter->setBrush(brush);
painter->setRenderHints(QPainter::Antialiasing, true);
QRectF rect(3, 3, m_outerDia, m_outerDia);
int startAngle = DEGREE_MULTIPLIER * TWELVE_O_CLOCK_DEGREE;
int spanAngle = DEGREE_MULTIPLIER * (m_tenth_percent * 360 / 1000);
painter->drawArc(rect, startAngle, spanAngle);
}
|
|
|
Source code |
1 2 3 4 |
QDeclarativeView view;
view.setSource(QUrl::fromLocalFile("MyItem.qml"));
view.show();
QObject *object = view.rootObject();
|
|
|
Source code |
1 2 |
object->setProperty("width", 500);
QDeclarativeProperty(object, "width").write(500);
|
aint(painter * p)This post has been edited 2 times, last edit by "RonMexico" (May 19th 2012, 8:04pm) with the following reason: (fixed typos, and for clarity)
This post has been edited 1 times, last edit by "RonMexico" (May 19th 2012, 8:10pm)
This post has been edited 2 times, last edit by "RonMexico" (May 20th 2012, 1:15pm) with the following reason: typos
|
|
Source code |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
#include <vector>
#include <algorithm>
#include <string>
#include <iostream>
#pragma region interfaces
template<class T>
class IVectorProvider
{
public:
virtual std::vector<T> const * getVector() const = 0;
};
template<class T>
class IGuiDataProvider
{
public:
typedef std::vector<T> const * TDataContainer;
virtual TDataContainer getGuiData() const = 0;
};
class IDrawable
{
public:
virtual void draw() = 0;
};
#pragma endregion
template <class T>
class Data : public IVectorProvider<T>
{
public:
void addDataElement(T const & elem)
{
m_data.push_back(elem);
}
std::vector<T> const * getVector() const
{
return &m_data;
}
private:
std::vector<T> m_data;
};
class Adapter : public IGuiDataProvider<int>
{
public:
Adapter(IVectorProvider<int> * const provider)
: m_dataSource(provider)
{}
virtual std::vector<int> const * getGuiData() const
{
return m_dataSource->getVector();
}
private:
IVectorProvider<int> const * m_dataSource;
};
class GuiThing : public IDrawable
{
public:
GuiThing(std::string const & name, IGuiDataProvider<int> const * dataProvider)
: m_name(name), m_guiDataHandle(dataProvider)
{}
void draw()
{
useData();
}
private:
void useData()
{
IGuiDataProvider<int>::TDataContainer container;
container = m_guiDataHandle->getGuiData();
std::cout << m_name << '\n';
for(int i = 0; i < container->size(); ++i)
{
std::cout << container->at(i) << '\n'; // use the data
}
}
private:
IGuiDataProvider<int> const * m_guiDataHandle;
std::string m_name;
};
int main()
{
Data<int> data;
data.addDataElement(1);
data.addDataElement(2);
data.addDataElement(3);
Adapter adapter(&data);
GuiThing gt1("one", &adapter);
GuiThing gt2("two", &adapter);
GuiThing gt3("three", &adapter);
GuiThing gt4("four", &adapter);
std::vector<IDrawable*> drawables;
drawables.push_back(>1);
drawables.push_back(>2);
drawables.push_back(>3);
drawables.push_back(>4);
std::for_each(drawables.begin(), drawables.end(), std::mem_fun(&IDrawable::draw));
std::cin.get();
}
|
This post has been edited 2 times, last edit by "RonMexico" (May 20th 2012, 4:32pm) with the following reason: typo
We're back to agreeing to disagree. You're passing pointers to a collection just like I'm doing. The content of the items must be agreed on between viewer and database just like mine. The adapter doesn't do much for you except allow you to write other data
sources for the same view -- if you will never need to do that (I
won't) it's needless complexity.
If you change the type of the items in the collection, you have to change what you do with it (i.e., within your unspecified "use the data"). That's my point -- you can add abstractions to separate it physically all you want, but the database and the data viewer are coupled practically, in the real world. If you change your database in a way thats important to that view -- its the same amount of work inside "use the data"
Quoted
Also I note that you changed your position a little. You said a few posts ago:
"The canvas should save whatever subset of this data (that it seems not to own) it needs for customised painting."
Quoted
That is what I most sharply disagree with. What you propose below is fine, but not automatically "better practice" than mine.
|
|
Source code |
1 2 3 4 5 6 7 8 9 |
QDeclarativeView view;
view.setSource(QUrl::fromLocalFile("MyItem.qml"));
view.show();
QObject *object = view.rootObject();
// *** todo ***
// cast the object to its 'real' type
// make the data (and any adapter)
// call some setup method on object passing the appropriate args.
|