当使用STL的函数时,sort()或者min_element()我总是必须明确地指定开始和结束的范围:
void range_example()
{
std::vector<int> list = {7, 3, 9, 1, 5, 2};
auto found_element = std::min_element(list.begin(), list.end());
std::cout << *found_element << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
如果我打算只在我的容器的一部分上工作,这是有道理的,但更多时候我需要函数来处理整个容器.有没有一个原因,为什么没有一个重载函数允许这样做:
std::vector<int> list = {7, 3, 9, 1, 5, 2};
auto found_element = std::min_element(list);
Run Code Online (Sandbox Code Playgroud)
有没有办法完成我忽略的容器总范围的函数调用?
编辑:我知道我可以自己将其封装在一个函数中,但是因为必须对所有函数进行此操作,所以如果有更好的方法,我希望避免这种情况.
我想写一个小的HTTP服务器应用程序,它接收HTTP GET请求,处理它们并发出回复.由于应用程序的历史,我赞成使用Qt,但我能找到的是另一个(更常见的)方向:向服务器发送请求并使用QNetworkAccessManager接收回复.我需要的是一个类似socket的东西,当一个请求进来时,会生成一个对象,我可以在这个对象中选择url等,这样我就可以发出适当的回复.
在Qt框架中,我只是盲目或者不是这样吗?如果是这样,你能推荐替代品吗?
是否记录了任何已知的尺寸/空间限制QPixmap和/或QImage对象?我没有找到任何有用的信息.我目前在OSX和Windows上使用Qt 4.7.3.特别是我对以下内容感兴趣:
我天真地怀疑记忆是唯一的限制因此,人们可以通过计算最大尺寸
width x height x byte_per_pixel
我认为有一个更精细的经验法则; 当你遇到GB尺寸时,32位机器也可能有解决问题.
最后,我希望存储大小约为16000x16000像素的多个RGBA图像,并在a中使用透明度将它们渲染到彼此QGraphicsScene.可用的工作站可以有很多RAM,比方说16GB.
tl; dr:您了解QImage/QPixmap的大小限制,或者我在哪里可以找到此类信息?
编辑:我知道平铺方法,我很好.知道上述事情仍然会很棒.
谢谢!
我正在构建一个小型的多线程Web服务器.QTcpSockets在主线程中获取,然后由QtConcurrent传递给QThreadPool,QThreadPool最终处理数据并发出答案.
我的问题是套接字是在主线程中创建的,并在另一个线程中处理.这在尝试写入套接字时会导致错误:
socket->write(somedata);
Run Code Online (Sandbox Code Playgroud)
QObject:无法为位于不同线程中的父级创建子级.(Parent是QNativeSocketEngine(0x608330),父线程是QThread(0x600630),当前线程是QThread(0x505f60)
干净的方法是使用将套接字对象移动到处理线程
socket->moveToThread(QThread::currentThread()).
Run Code Online (Sandbox Code Playgroud)
但是,这只能在创建对象的线程中调用.此外,套接字将QTcpServer对象作为父对象,因此moveToThread()无论如何都会失败(父对象无法切换线程).
如何将对象移动到线程池运行的代码中的QThread :: currentThread()?或者,我如何写入创建它的线程之外的套接字?
C++不允许基于返回类型的方法的多态性.但是,当重载隐式转换成员函数时,这似乎是可能的.
有谁知道为什么?我认为运算符在内部处理方式.
编辑:这是一个例子:
struct func {
operator string() { return "1";}
operator int() { return 2; }
};
int main( ) {
int x = func(); // calls int version
string y = func(); // calls string version
double d = func(); // calls int version
cout << func() << endl; // calls int version
}
Run Code Online (Sandbox Code Playgroud) 当我尝试编译此代码时:
struct BasicVertexProperties
{
Vect3Df position;
};
struct BasicEdgeProperties
{
};
template < typename VERTEXPROPERTIES, typename EDGEPROPERTIES >
class Graph
{
typedef adjacency_list<
setS, // disallow parallel edges
vecS, // vertex container
bidirectionalS, // directed graph
property<vertex_properties_t, VERTEXPROPERTIES>,
property<edge_properties_t, EDGEPROPERTIES>
> GraphContainer;
typedef graph_traits<GraphContainer>::vertex_descriptor Vertex;
typedef graph_traits<GraphContainer>::edge_descriptor Edge;
};
Run Code Online (Sandbox Code Playgroud)
g ++抱怨"typedef graph_traits <>"行中出现以下错误:
error: type 'boost::graph_traits<boost::adjacency_list<boost::setS, boost::vecS,
boost::bidirectionalS, boost::property<vertex_properties_t, VERTEXPROPERTIES,
boost::no_property>, boost::property<edge_properties_t, EDGEPROPERTIES,
boost::no_property>, boost::no_property, boost::listS> >' is not derived from type
'Graph<VERTEXPROPERTIES, EDGEPROPERTIES>'
Run Code Online (Sandbox Code Playgroud)
我发现编译器似乎不知道我的模板参数是类型,但在属性定义中将"typename"放在它们之前并没有帮助.
怎么了?我只是希望有一个模板化的Graph类,可以使用我喜欢的任何属性,从上面定义的基本属性结构派生,所以我可以在这个Graph中使用基本属性的方法.
我有一个画布,其中包含一些可以拖动的MouseAreas.画布在这些项目之间绘制一些装饰,这些装饰不能表示为本机QML元素.
当项目改变大小或位置时,如何重新绘制画布?我知道我需要调用canvas.requestPaint(),但是在哪个事件上呢?到目前为止我找到的唯一解决方案是在一个计时器中调用requestPaint(),但是由于性能原因我想避免这种情况.
我有一个QQuickView显示QML文件,它本身由几个QML项目(在单独的文件中)组成.我想使用C++代码动态添加Items.动态添加的项目应与父一个调整大小,即width和height属性参考的父.
例如,我在QML中的目标Item看起来像这样:
// TargetContainer.qml
Grid {
id: fillMeWithItemsContainer
objectName: "fillMeWithItemsContainer"
}
Run Code Online (Sandbox Code Playgroud)
我想动态添加的项目(可能是多次)看起来像这样:
// DynamicItem.qml
Rectangle {
color: "white"
height: fillMeWithItemsContainer.height
width: height * 4/3
}
Run Code Online (Sandbox Code Playgroud)
请注意,矩形引用了有关高度的容器.
quickView填充有TargetContainer:
QQuickView *quickView = new QQuickView();
quickView->setSource(QUrl("qrc:/foo/bar/TargetContainer.qml"));
Run Code Online (Sandbox Code Playgroud)
所以我加载了一个组件
QQmlComponent dynamicallyLoadedComponent(
quickView->engine(),
QUrl("qrc:/foo/bar/DynamicItem.qml")
);
Run Code Online (Sandbox Code Playgroud)
我创建了一个Object.
QObject *dynamicallyLoadedObject = dynamicallyLoadedComponent.create();
Run Code Online (Sandbox Code Playgroud)
这里我得到一个错误(在应用程序输出视图中):
DynamicItem.qml:4: ReferenceError: fillMeWithItemsContainer is not defined
Run Code Online (Sandbox Code Playgroud)
quickView应该知道fillMeWithItemsContainer它的存在,因为它之前已经被创造过了.但是,fillMeWithItemsContainer不是dynamicallyLoadedObject(还)的父母,这可能是问题所在.
所以我找到了目标项目
QQuickItem *targetItem = quickView->rootObject()->findChild<QQuickItem*>("fillMeWithItemsContainer");
Run Code Online (Sandbox Code Playgroud)
并重新显示以前创建的对象
dynamicallyLoadedObject->setProperty("parent", QVariant::fromValue<QObject*>(targetItem ));
Run Code Online (Sandbox Code Playgroud)
注意:我dynamicallyLoadedObject->setParent() …