方案:
在add_executable()语句中添加资源文件
(不在add_library())
无法设置主窗口图标.
备注:
当我不使用时,AUTORCC我遇到了一些编译问题:
QtCore/qglobal.h: no such file or directory.但是,我更喜欢AUTORCC作为一种更现代的CMake方法.
如果没有AUTORCC(与提供的CMakeLists.txt不同)和Qt-4.6.2,则当前代码无效.不同的CMakeLists.txt)
这是我项目的最小化代码.树:
|- CMakeLists.txt
|- main_window.hpp
|- main_window.cpp
|- main.cpp
|- resources
| - resources.qrc
| - images
| - logo.png
Run Code Online (Sandbox Code Playgroud)
main_window.cpp
#ifndef MAINWINDOW_HPP
#define MAINWINDOW_HPP
#include <QMainWindow>
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow();
};
#endif
Run Code Online (Sandbox Code Playgroud)
main_window.cpp
#include "main_window.hpp"
MainWindow::MainWindow()
{
// i tried ":/images.png", ":/resources/images/logo.png", ":/logo.png"
setWindowIcon(QIcon(":images/logo.png"));
}
Run Code Online (Sandbox Code Playgroud)
main.cpp中
#include <QApplication>
#include "main_window.hpp" …Run Code Online (Sandbox Code Playgroud) 我正在为我的应用程序的日志记录平台使用Boost-Log和全局严重性记录器.分析表明,boost::log::v2s_mt_posix::core::open_record最多可占用总执行时间的25%.
我确实有很多日志消息,但我不希望它们如此昂贵,因为它们的严重性较低,而且它们被过滤掉了.
有没有办法让这些消息在运行时不那么昂贵?(再次:我希望即使在过滤时也会有一个小的开销,当然,当没有过滤时,它会更大).
通过创建一些包装器宏,编译时相对容易"修复".
更新样本工作代码:
#include <cmath>
#include <iostream>
#include <string>
#include <boost/lexical_cast.hpp>
#include <boost/log/sources/global_logger_storage.hpp>
#include <boost/log/sources/severity_logger.hpp>
#include <boost/log/trivial.hpp>
#include <boost/log/sources/severity_channel_logger.hpp>
#include <boost/log/common.hpp>
#include <boost/log/expressions.hpp>
#include <boost/log/sinks.hpp>
#include <boost/log/utility/setup/common_attributes.hpp>
#include <boost/log/utility/setup/console.hpp>
#include <boost/log/support/date_time.hpp>
#include <boost/utility/empty_deleter.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/make_shared.hpp>
// severity levels
enum severity_level {INFO};
// Logging macro
#define LOG(level) BOOST_LOG_SEV(global_logger::get(), level)
// Initializing global boost::log logger
typedef boost::log::sources::severity_channel_logger_mt<
severity_level, std::string> global_logger_type;
BOOST_LOG_INLINE_GLOBAL_LOGGER_INIT(global_logger, global_logger_type)
{
return global_logger_type(boost::log::keywords::channel = "global_logger");
}
// Initialize terminal logger
void …Run Code Online (Sandbox Code Playgroud) 以下代码与boost 1.57一样正常工作:
#include <iostream>
#include <boost/log/trivial.hpp>
struct Foo
{
int d=1;
};
std::ostream& operator<<(std::ostream& out, const Foo& foo)
{
out << "Foo: " << foo.d;
return out;
}
int main()
{
BOOST_LOG_TRIVIAL(info) << Foo();
return EXIT_SUCCESS;
}
Run Code Online (Sandbox Code Playgroud)
使用boost 1.59相同的代码失败.第一个gcc错误消息是:
错误:'operator <<'不匹配(操作数类型为'boost :: log :: v2s_mt_posix :: basic_record_ostream'和'Foo')
文档和发行说明均未记录需要更改的内容.
我有经典(可能有问题的)多重继承钻石计划.
我想有一个std::vector可以包含两种C或D
对象,所以我做它作为std::vector<C>其D的爸爸和正常工作.
但是,当我使用:std::vector<std::unique_ptr<C>>然后我在矢量的破坏时有分段错误.
** glibc detected *** ./a.out: free(): invalid pointer: 0x0000000009948018***
Run Code Online (Sandbox Code Playgroud)
为什么会有区别?对我来说,即使是第一次实施也是有问题的.
码
#include <string>
#include <vector>
#include <memory>
class A
{
public:
A() = default;
};
class B : public virtual A
{
public:
B() = default;
};
class C : public virtual A
{
public:
C() = default;
};
class D : public B, public C
{
public:
D() = default; …Run Code Online (Sandbox Code Playgroud) 最近,当我实现一个类时,我创建了一个名为运算符的嵌套命名空间,我在其中添加了流操作符.
我之所以这样做是因为我经常需要在名称命名空间我以外的命名空间中使用它们
using my_namespace::operators;
Run Code Online (Sandbox Code Playgroud)
就在我想要的地方,就是这样.
这里我有一个类Point,一个类Segment及其流运算符的示例,其中Segment的流调用Point的流.但是......我无法编译:
分数点:
#ifndef POINT_HPP
#define POINT_HPP
#include <iostream>
namespace geom {
class Point
{
public:
Point(int x_, int y_) : x(x_), y(y_) {};
int x;
int y;
};
namespace operators {
std::ostream& operator<<(std::ostream& out, const Point& p)
{
out << "(" << p.x << ", " << p.y << ")";
return out;
}
} // ~ namespace geom::operators
} // ~ namespace geom
#endif // ~ POINT_HPP
Run Code Online (Sandbox Code Playgroud)
类别细分:
#ifndef SEGMENT_HPP
#define SEGMENT_HPP
#include …Run Code Online (Sandbox Code Playgroud) c++ ×5
boost ×2
boost-log ×2
logging ×2
c++11 ×1
cmake ×1
inheritance ×1
linux ×1
namespaces ×1
qt ×1
qt4 ×1
unique-ptr ×1
vector ×1