我想出了如何在QML中公开和绑定QAbstractListModel派生的listmodel的实例.
但我真正想做的是将一个对象暴露给QML并将一个QAbstractListModel派生的listmodel成员绑定为Q_PROPERTY.
我试过这种方式:
class MyObject : public QObject
{
Q_OBJECT
Q_PROPERTY(MyListModel myListModel READ myListModel NOTIFY myListModelChanged)
public:
explicit MyObject(QObject *parent = 0);
MyListModel *myListModel();
signals:
void myListModelChanged();
public slots:
private:
MyListModel *m_myListModel;
};
MyObject::MyObject(QObject *parent) :
QObject(parent)
{
m_myListModel = new MyListModel(this);
}
MyListModel *MyObject::myListModel()
{
return m_myListModel;
}
class MyListModel : public QAbstractListModel {
Q_OBJECT
//...
//...
}
int main(int argc, char *argv[])
{
QGuiApplication a(argc, argv);
QQuickView *view = new QQuickView();
MyObject *myObject = new MyObject();
view->engine()->rootContext()->setContextProperty("myObject", myObject); …Run Code Online (Sandbox Code Playgroud) 我已阅读此博文http://www.ics.com/blog/qt-tips-and-tricks-part-1并尝试按照所述启用插件调试。
我已将此行放入我的 main.cpp 中:
qputenv(QT_DEBUG_PLUGINS, 1);
但是如果我尝试编译我会收到此错误:
.../src/main.cpp:14: error: 'QT_DEBUG_PLUGINS' was not declared in this scope
qputenv(QT_DEBUG_PLUGINS, -1);
Run Code Online (Sandbox Code Playgroud)
这里有什么问题,我该如何做才能正确?
qputenv("QT_DEBUG_PLUGINS", QByteArray("1"));
Run Code Online (Sandbox Code Playgroud)
但我没有得到任何额外的输出。
我在 KUbuntu 15.10 下使用 Qt5.5.1 和 QtCreator 3.6。
我正在尝试将QScopedPointers存储在QList中.
我发现了这个评论
也可以使用QList>. - Kuba Ober 2014年1月14日18:17
(首先评论这个答案:https://stackoverflow.com/a/21120575/3095014)
这篇文章https://forum.qt.io/topic/59338/solved-qlist-of-qscopedpointers暗示这应该有效.但是,如果我尝试编译第二个链接的代码,我会收到此错误:
E:\Qt\Qt5Enterprise\5.5\msvc2013\include\QtCore/qlist.h(404) : error C2248: 'QScopedPointer<Label,QScopedPointerDeleter<T>>::QScopedPointer' : cannot access private member declared in class 'QScopedPointer<Label,QScopedPointerDeleter<T>>'
with
[
T=Label
]
E:\Qt\Qt5Enterprise\5.5\msvc2013\include\QtCore/qscopedpointer.h(170) : see declaration of 'QScopedPointer<Label,QScopedPointerDeleter<T>>::QScopedPointer'
with
[
T=Label
]
E:\Qt\Qt5Enterprise\5.5\msvc2013\include\QtCore/qlist.h(403) : while compiling class template member function 'void QList<QScopedPointer<Label,QScopedPointerDeleter<T>>>::node_construct(QList<QScopedPointer<T,QScopedPointerDeleter<T>>>::Node *,const QScopedPointer<T,QScopedPointerDeleter<T>> &)'
with
[
T=Label
]
E:\Qt\Qt5Enterprise\5.5\msvc2013\include\QtCore/qlist.h(553) : see reference to function template instantiation 'void QList<QScopedPointer<Label,QScopedPointerDeleter<T>>>::node_construct(QList<QScopedPointer<T,QScopedPointerDeleter<T>>>::Node *,const QScopedPointer<T,QScopedPointerDeleter<T>> &)' being compiled
with
[
T=Label
]
E:\Qt\Qt5Enterprise\5.5\msvc2013\include\QtCore/qlist.h(794) …Run Code Online (Sandbox Code Playgroud) 如果我有一个 QVector,我可以使用基于范围的循环,使用引用并更改 QVector 中的对象。
但如果我在修改对象时需要索引,我必须使用普通的 for 循环。但是我怎样才能改变 QVector 中对象的值呢?
作为解决方法,我在更改临时对象后使用了替换方法,但这有点难看。
这是代码:
struct Resource {
int value = 0;
};
int main(int argc, char *argv[])
{
QVector<Resource> vector{Resource{}, Resource{}, Resource{}};
qDebug() << vector.at(0).value
<< vector.at(1).value
<< vector.at(2).value;
for(Resource &res : vector)
res.value = 1;
qDebug() << vector.at(0).value
<< vector.at(1).value
<< vector.at(2).value;
for(int i = 0; i < vector.size(); ++i) {
//Resource &res = vector.at(i); <-- won't compile: cannot convert from 'const Resource' to 'Resource &'
Resource &res = vector.value(i); //Compiles, but …Run Code Online (Sandbox Code Playgroud) 我想要的是两种不同的整数类型,它们在语义上是可区分的.
例如,在此代码中,"Meter"类型和"Pixel"int类型
typealias Meter = Int
typealias Pixel = Int
fun Meter.toPixel() = this * 100
fun Pixel.toMeter() = this / 100
fun calcSquareMeters(width: Meter, height: Meter) = width * height
fun calcSquarePixels(width: Pixel, height: Pixel) = width * height
fun main(args: Array<String>) {
val pixelWidth: Pixel = 50
val pixelHeight: Pixel = 50
val meterWidth: Meter = 50
val meterHeight: Meter = 50
calcSquareMeters(pixelWidth, pixelHeight) // (a) this should not work
pixelWidth.toPixel() // (b) this should not work
} …Run Code Online (Sandbox Code Playgroud) 我想从 Windows 切换到 Linux 进行 python 开发。因为我想使用 pyside,所以我的 64 位 Kubuntu 下需要一个 32 位 python。
在 64 位 Windows 下运行 32 位 python 是轻而易举的事。在 Linux 下,它似乎要困难得多,这让我有点累。
我尝试按照本指南进行操作:https : //stackoverflow.com/a/5507373
但我被困在这里
user1@user1-desktop:~/src/virtualenv-1.5.2/virtualenvs$ ~/.localpython/bin/virtualenv py2.7 --python=/home/user1/.localpython/bin/python2.7
Traceback (most recent call last):
File "/home/user1/.localpython/bin/virtualenv", line 2, in <module>
import virtualenv
File "/home/user1/.local/lib/python2.7/site-packages/virtualenv.py", line 17, in <module>
import zlib
ImportError: No module named zlib
Run Code Online (Sandbox Code Playgroud)
做sudo apt-get install zlib1g-dev和重新编译 python 没有帮助。
那么如何解决这个问题呢?