Git Product home page Git Product logo

Comments (14)

Mooophy avatar Mooophy commented on June 1, 2024

用某个api,传递坐标进去?

from qtlab.

pezy avatar pezy commented on June 1, 2024

用某个api,传递坐标进去?

肯定需要传坐标,问题在于这个线性插值是通过将 QLinearGradient 传入QBush 实现的,也就是插入几个颜色,就可以生成连贯的一个线性渐变。如

QLinearGradient linearGrad(QPointF(100, 100), QPointF(600, 100));
linearGrad.setColorAt(0, Qt::black);
linearGrad.setColorAt(1, Qt::white);

就能绘制出一个从白到黑的渐变条:(图文不符,重在示意)
example

但我想知道某个位置具体的颜色,却没有现成的 API 了。(没有类似 getColor(pos) 这种API)
故有这样的疑问。

from qtlab.

Mooophy avatar Mooophy commented on June 1, 2024

我能想到的。。就比较笨:
QLinearGradient linearGrad(QPointF(100, 100), QPointF(600, 100));中的(100,100)(600,100)·`是确定一个线段吧?那么鼠标右键的点击事件也可以得到一个坐标。找到鼠标对这条线段的相对位置,就可以换算出该点黑色的程度,就可以换算出该点的颜色了吧?
不过貌似挺笨的。。

from qtlab.

pezy avatar pezy commented on June 1, 2024

找到鼠标对这条线段的相对位置,就可以换算出该点黑色的程度

我也是这样想的,不笨吧。。。笨吗? 我卡在 “黑的程度” 如何计算上了。RGB三个值,是一个三维数组。

from qtlab.

pezy avatar pezy commented on June 1, 2024

qq 20141022153927

最终还是用了Qt的API, 擦,Qt的基础库堪比工业级的STL了。。。连线性插值算法都内置了。

from qtlab.

Mooophy avatar Mooophy commented on June 1, 2024

对呀,我就觉得有现成的api,所以才会觉得自己换算会比较笨。。C++的标准化是1998。。SGI STL首发是1997,而Qt首发是1995。换句话说。。Qt比STL还老牌。。

from qtlab.

pezy avatar pezy commented on June 1, 2024

这么看的确是,我最喜欢Qt的地方在于,它的基础库和STL是无缝兼容的。如QMap,QVector之类。都是继承了std::map 和 std::vector 而来的。当然它优化的很厉害,用起来没STL(至少是C++0x)那么别扭。

from qtlab.

Mooophy avatar Mooophy commented on June 1, 2024

QMap,QVector之类。都是继承了std::map 和 std::vector 而来的。居然是继承的?你是说oo那个继承吗?

from qtlab.

pezy avatar pezy commented on June 1, 2024

居然是继承的?你是说oo那个继承吗?

不不不,不是 class QMap : public std::map.这么简单。怎么说呢,QMap和std::map用的是同一个iterator 不对不对,被模板搞晕了。OK, 两者没啥关系,只是有一个接口toStdMap罢了。而且QMap的构造函数可以是std::map.

代码:

template <class Key, class T>
Q_OUTOFLINE_TEMPLATE QMap<Key, T>::QMap(const std::map<Key, T> &other)
{
    d = QMapData::createData(alignment());
    d->insertInOrder = true;
    typename std::map<Key,T>::const_iterator it = other.end();
    while (it != other.begin()) {
        --it;
        insert((*it).first, (*it).second);
    }
    d->insertInOrder = false;
}

template <class Key, class T>
Q_OUTOFLINE_TEMPLATE std::map<Key, T> QMap<Key, T>::toStdMap() const
{
    std::map<Key, T> map;
    const_iterator it = end();
    while (it != begin()) {
        --it;
        map.insert(std::pair<Key, T>(it.key(), it.value()));
    }
    return map;
}

但 QMap是一个独立的模板类。 这里面设计挺巧妙,我看了源码也说不太好。
别被我误导,没有继承,没有继承。

反正,对于用户来说,以前使用STL的代码,无需重写,就可以和Qt的代码天然融合了。

from qtlab.

Mooophy avatar Mooophy commented on June 1, 2024

嗯嗯,不存在误导的问题,你不说起来,我也从没想过这个问题。只是印象里Qt这种框架OO程度应该比较高才对。而印象里std的容器不提供virtual destructor,一旦直接继承,Qt的容器也无法实现多态了,你查下Qt容器的析构函数,看看是不是virtual的。

这种东西越讨论越清晰,不会误导的~

from qtlab.

pezy avatar pezy commented on June 1, 2024

很奇怪,它的构造与析构是这样的:

inline QMap() : d(&QMapData::shared_null) { d->ref.ref(); }
inline QMap(const QMap<Key, T> &other) : d(other.d)
{ d->ref.ref(); if (!d->sharable) detach(); }
inline ~QMap() { if (!d) return; if (!d->ref.deref()) freeData(d); }

不过谢你提示,我第一次看 Qt Core 里的源码,这些容器实现的好干净啊。比 Windows 下的 STL 源码好看十倍不止。。。

对了,设计这种基础库,才是模板与泛型的用武之地。你这方面这么强,应该可以考虑搞个 WMap 之类的东西。估计设计起来也挺有挑战性的。

from qtlab.

Mooophy avatar Mooophy commented on June 1, 2024

话说源码哪找到的?可读性居然这么好。。STL根本就不是给人看的。。
话说QMap是继承结构里的最顶层吗?如果有父类的话,看下父类的析构函数是否virtual。。如果都不virtualQMap也不能动多态,靠的是模板在编译时间实现静多态,这就和STL容器一致了。也否定了Qt容器能实现动多态的猜测。。同时,继承Qt容器时也不能加虚函数。

from qtlab.

pezy avatar pezy commented on June 1, 2024

😆 你用了这么久的QCreator 难道不知道它们都是开源的么。。。https://qt.gitorious.org/

可读性相当赞,我学到不少代码习惯和技巧。用QCreator看更好一些,可以直接定位到源文件,我用VS看,只能定位到.h貌似,需要手动找.cpp。

QCreator中,我记得Ctrl + 鼠标,就可以直接到源码。

另外,QMap是顶级了,没有父类。应该是静多态的。

from qtlab.

Mooophy avatar Mooophy commented on June 1, 2024

我真对不住QtCreator。。

from qtlab.

Related Issues (12)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.