我试图显示一个图像QAbstractTableModel.我试图返回QPixmap作为QVariant的data(),但它只是产生空单元格,当我希望在第二列的每一个细胞有一个20×20的黑色正方形.
这是我目前的代码:
QVariant MySqlTableModel::data(const QModelIndex &idx, int role = Qt::DisplayRole) const
{
if (role == Qt::DisplayRole && idx.column() == 1) {
QPixmap pixmap(20,20);
QColor black(0,0,0);
pixmap.fill(black);
return pixmap;
}
return QSqlTableModel::data(idx, role);
}
Run Code Online (Sandbox Code Playgroud) 我希望在特定列表视图项目悬停时执行某些操作.我尝试了输入的(QModelIndex)信号,但是当光标移动到项目上时,它似乎没有发出(即使它们在光标移过它们时突出显示).是否有不同的信号表明这一点?我只看到激活(),点击()和按下()这里似乎相关的http://qt-project.org/doc/qt-4.8/qabstractitemview.html#signals.
有没有办法使layoutStretch属性始终被遵守?例如,我将它设置为"1,3,2",但是第一部分中的小部件(标签)("1,3,2"中的"1")扩展(当添加更多文本时),以及那么1:3:2的配给量就不再受到尊重了.也就是说,"1:3:2"比率变成更像"3:1:3"的东西.
如果我有这样的xml文件:
<root>
<item>
<prop>something</prop>
</item>
<test>
<prop>something</prop>
</test>
<test2>
<prop>something</prop>
</test2>
</root>
Run Code Online (Sandbox Code Playgroud)
我可以
xmlTree.getroot().findall("item")
用来获取所有'item'元素.
我如何获得所有'item'或'test'元素?我想要的东西:
xmlTree.getroot().findall("item or test")
我在文档的示例中没有看到这样的内容.有任何想法吗?
我想确保static_assert在单元测试中正常工作.也就是说,如果我有一个
class MyClass {static_assert(my_type_trait<T>::value, "error"); };
Run Code Online (Sandbox Code Playgroud)
然后在单元测试中MyClass<TypeWithTrait> myClass;应该"通过"并且MyClass<TypeWithoutTrait> myClass;应该"失败".
可以这样做吗?
似乎没有multi_array移动构造函数 - 这是正确的吗?这是否有原因,或者它只是从未实现,因为该类似乎是在移动语义可用之前编写的?在用户领域有什么可以做的吗?
我读到使用一个将在紧密循环中调用的函数的策略类比使用多态函数快得多.但是,我设置了这个演示,时间表明它正好相反!?策略版本比多态版本长2-3倍.
#include <iostream>
#include <boost/timer.hpp>
// Policy version
template < typename operation_policy>
class DoOperationPolicy : public operation_policy
{
using operation_policy::Operation;
public:
void Run(const float a, const float b)
{
Operation(a,b);
}
};
class OperationPolicy_Add
{
protected:
float Operation(const float a, const float b)
{
return a + b;
}
};
// Polymorphic version
class DoOperation
{
public:
virtual float Run(const float a, const float b)= 0;
};
class OperationAdd : public DoOperation
{
public:
float Run(const float a, const float …Run Code Online (Sandbox Code Playgroud) 我知道实际上不处理在其他线程中引发的异常是没有意义的,但是有什么方法可以通知我至少发生了异常?例如类似
#include <QtConcurrentRun>
#include <iostream>
#include <stdexcept>
void MyFunction()
{
// std::cout << "MyFunction()" << std::endl;
throw std::runtime_error("Test exception.");
}
int main()
{
try
{
QtConcurrent::run(MyFunction);
}
catch(...)
{
std::cout << "Exception caught!" << std::endl;
}
}
Run Code Online (Sandbox Code Playgroud)
即使发生异常,也会安静地退出。当异常来自某个调用堆栈深处的异常时,这有时会造成混乱。
- - - - - - 编辑 - - - - - - -
我试图写一个像UmNyobe建议的包装器,但是我对函数指针做错了吗?
#include <QtConcurrentRun>
#include <QFutureWatcher>
#include <QObject>
#include <iostream>
#include <stdexcept>
void MyFunction()
{
// std::cout << "MyFunction()" << std::endl;
throw std::runtime_error("Test exception.");
}
template<typename TFirstParam, typename... TParams> …Run Code Online (Sandbox Code Playgroud) 我有两个有效的多边形。当我接受他们的并集时,会得到一个无效的多边形(存在自相交)。这是错误吗?我希望联合操作将始终产生有效的多边形。我在下面提供了可视化示例。谁能解释为什么这不是错误,或者是否有办法解决?
#include <fstream>
#include <iostream>
#include <boost/geometry.hpp> // read_wkt
#include <boost/geometry/geometries/point_xy.hpp>
#include <boost/geometry/geometries/polygon.hpp>
#include <boost/geometry/geometries/multi_polygon.hpp>
#include <boost/geometry/algorithms/union.hpp>
using PointType = boost::geometry::model::d2::point_xy<double>;
using PolygonType = boost::geometry::model::polygon<PointType>;
using MultiPolygonType = boost::geometry::model::multi_polygon<PolygonType>;
template <typename TPolygon>
void WritePolygonsToSVG(const std::vector<TPolygon>& polygons, const std::string& filename)
{
std::ofstream svg(filename);
boost::geometry::svg_mapper<PointType> mapper(svg, 400, 400);
for(unsigned int i = 0; i < polygons.size(); ++i) {
mapper.add(polygons[i]);
mapper.map(polygons[i], "fill:rgb(255,128,0);stroke:rgb(0,0,100);stroke-width:1");
}
}
int main(int, char**)
{
/// Create two polygons
PolygonType singlePolygon1;
PolygonType singlePolygon2;
boost::geometry::read_wkt("POLYGON((-52.8018 -26.744,-57.5465 -27.9916,-62.2844 -29.2642,-63.19 -26.066,-57.564 -24.5243,-53.7273 -23.3394,-52.8018 …Run Code Online (Sandbox Code Playgroud) 我有一个主要的c ++项目,我使用CMake来管理。设置cmake_install_prefix和配置后,它将生成makefile,然后可以使用非常标准的makefile进行构建和安装:
make
make install
Run Code Online (Sandbox Code Playgroud)
至此,我的二进制文件最终以结束cmake_install_prefix,而无需执行其他工作即可执行它们。最近,我在源代码树的几个位置添加了一些Python脚本,其中一些依赖于其他脚本。我可以使用CMake将Python文件和目录结构复制到cmake_install_prefix,但是如果我进入该路径并尝试使用其中一个脚本,Python将找不到其他脚本,imports因为PYTHONPATH它不包含cmake_install_prefix。我知道您可以使用CMake设置环境变量,但是它不会在各个shell之间持久存在,因此,对于用户而言,它不是真正的“设置”,而不仅仅是当前的终端会话。
解决方案似乎是在您的软件构建说明中增加一个步骤,说“设置您的PYTHONPATH”。有什么办法可以避免这种情况?这是作为较大项目的一部分“安装” Python脚本的标准做法吗?设置项目的持续集成似乎确实使事情复杂化,因为必须手动配置诸如Jenkins之类的东西才能注入环境变量,而构建和执行从c ++代码生成的可执行文件并不需要任何特殊的东西。
c++ ×5
qt ×4
qt4 ×4
c++11 ×3
boost ×2
python ×2
cmake ×1
elementtree ×1
exception ×1
googletest ×1
jenkins ×1
oop ×1
unit-testing ×1