这是Qt信号/插槽连接的工作方式:
还有一个实际上是默认值的附加值:自动连接如果信号在接收对象具有亲和力的线程中发出,则其行为与直接连接相同。否则,行为与排队连接相同。”
在大多数情况下,默认设置可以正常运行:
但是,从线程发出信号时,您有两个选项来处理它:“已排队”或“阻止已排队”连接。
为什么没有这样的模式:
因为,正如文档所提到的,在同一线程中使用阻塞队列连接会导致死锁....所以处理起来确实很痛苦,我经常不得不在我的代码中创建和管理两个信号和连接来处理:
class A
{
Q_OBJECT
public:
A()
{
connect( this, SIGNAL(changedFromThread()), this, SLOT(update()), Qt::BlockingQueuedConnection );
connect( this, SIGNAL(changedNotFromThread()), this, SLOT(update()) );
}
void notifySomethingChanged()
{
if ( QObject().thread() != thread() )
emit changedFromThread(); // would dead-lock if in same thread
else
emit changedNotFromThread();
}
public slots:
void update()
{
// Do some changes to A that cannot be done …Run Code Online (Sandbox Code Playgroud) 我有一个基于 CMake 3.11.4 和 Python 3.7 的软件环境。
我的库/程序有一个config.txt文件,以我指定的格式描述它们的依赖关系。然后,我有一个 Python 脚本 ( scripts/configure.py),它会动态生成CMakeLists.txt,然后调用 CMake 来生成可由 Visual Studio 2015 构建的解决方案。
我希望 Pythonconfig.txt在用户编辑时自动再次运行。
所以我让我的 Python 脚本在生成的CMakeLists.txt. 以下是名为“myproject”的项目的外观,其中包括两个库“lib1”和“lib2”。
${SDE_ROOT_DIR}/build/myproject/CMakeLists.txt 包含:
# Automatically re-run configure project when an input file changes:
set( PROJECT_DEPENDENCIES )
list( APPEND PROJECT_DEPENDENCIES "${SDE_ROOT_DIR}/lib/lib1/config.txt" )
list( APPEND PROJECT_DEPENDENCIES "${SDE_ROOT_DIR}/lib/lib2/config.txt" )
ADD_CUSTOM_COMMAND( OUTPUT ${SDE_ROOT_DIR}/build/myproject/CMakeLists.txt COMMAND tools/python/Python370/python.exe scripts/configure.py myproject WORKING_DIRECTORY ${SDE_ROOT_DIR} DEPENDS ${PROJECT_DEPENDENCIES} )
Run Code Online (Sandbox Code Playgroud)
这是我所做的:
scripts/configure.py myproject) 来CMakeLists.txt生成 Visual …我正在 Android 上部署一个 C++ 应用程序,它使用boost::date_time. 它有很多库,有些是在编译时链接的(共享库),有些是插件,在运行时通过dlopen. 在某些库中,将 a 设置boost::posix_time::time_facet为 a std::ostream(使用imbue)以自定义boost::posix_time::ptime显示无效(被忽略)。我可以在以下 MCVE 中隔离问题:
bug_datetime_base是一个共享库,它使用boost::date_time但只编译将 a 重定向boost::posix_time::ptime到 a 的代码std::ostream(未boost::posix_time::time_facet使用):
MyClass::MyClass( const boost::posix_time::ptime& timeInfo )
{
std::cout << timeInfo;
}
Run Code Online (Sandbox Code Playgroud)
bug_datetime_lib是一个共享库,它使用boost::date_time和导出一个函数,该函数将使用 a 将 aboost::posix_time::time_facet重定向boost::posix_time::ptime到std::ostream具有特定格式的 a:
#include <sstream>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <QDebug>
void TestBoost()
{
boost::posix_time::ptime t1(boost::gregorian::date(2002,boost::gregorian::Jan,10),
boost::posix_time::time_duration(1,2,4));
std::stringstream temp;
temp << "FROM TestBoost:" …Run Code Online (Sandbox Code Playgroud) 最后我安装了Ubuntu并设置了Qt + Valgrind来防止内存泄漏,这是我在Windows中无法做到的.所以我无法理解这段代码是否提供内存泄漏?事实上,Valgrind说我只有500多个问题,但没有泄漏.一世
#include <QWidget>
#include <QFrame>
#include <QVBoxLayout>
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QWidget * wdgt = new QWidget; //this line should be the cause of leakage
//if it exist (as far as i know)
QVBoxLayout *layout = new QVBoxLayout;
QFrame * frame = new QFrame;
frame->setFrameStyle(QFrame::Panel | QFrame::Plain);
frame->setLineWidth(5);
layout->addWidget(frame);
wdgt->setLayout(layout);
wdgt->setFixedSize(800,600);
wdgt->show();
return a.exec();
}
Run Code Online (Sandbox Code Playgroud) 我有一个嵌入了一些流的mkv文件:
Stream #0:0: Video: h264 (High), yuv420p(tv, smpte170m/smpte170m/bt709), 720x300, SAR 1:1 DAR 12:5, 25 fps, 25 tbr, 48003.07 tbn, 50 tbc (default)
Stream #0:1(tr): Audio: ac3, 48000 Hz, stereo, fltp, 192 kb/s (default)
Stream #0:2(fr): Subtitle: dvd_subtitle, 720x300
Run Code Online (Sandbox Code Playgroud)
在我的电脑上播放时,我可以看到字幕.从我的旧DVD/DIVX播放器播放时,我看不到字幕(其他mkv/avi工作正常).
所以我想改变字幕编码,看看是否有另一个可以用于我的DVD播放器.
ffmpeg -encoders报道:
S..... ssa ASS (Advanced SubStation Alpha) subtitle (codec ass)
S..... ass ASS (Advanced SubStation Alpha) subtitle
S..... dvbsub DVB subtitles (codec dvb_subtitle)
S..... dvdsub DVD subtitles (codec dvd_subtitle)
S..... mov_text 3GPP Timed Text subtitle
S..... srt SubRip subtitle …Run Code Online (Sandbox Code Playgroud) 在我将一些遗留代码从 win32 移植到 win64 之后,在我讨论了消除“可能丢失数据”警告的最佳策略是什么之后(摆脱“警告 C4267 可能丢失数据”的最佳策略是什么?)。我即将取代许多unsigned int通过size_t在我的代码。
但是,我的代码在性能方面至关重要(我什至无法在调试中运行它......太慢了)。
我做了一个快速的基准测试:
#include "stdafx.h"
#include <iostream>
#include <chrono>
#include <string>
template<typename T> void testSpeed()
{
auto start = std::chrono::steady_clock::now();
T big = 0;
for ( T i = 0; i != 100000000; ++i )
big *= std::rand();
std::cout << "Elapsed " << std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now() - start).count() << "ms" << std::endl;
}
int main()
{
testSpeed<size_t>();
testSpeed<unsigned int>();
std::string str;
std::getline( std::cin, str ); // pause
return 0; …Run Code Online (Sandbox Code Playgroud) 我正在比较计算随机数平均值的两种算法.
我想这里没有革命性的东西,我不是数学家,所以我不能在这两个算法上加上名字.
这是我的代码:
#include <iostream>
#include <iomanip>
#include <cstdlib>
class Average1
{
public:
Average1() : total( 0 ), count( 0 ) {}
void add( double value )
{
total += value;
count++;
}
double average()
{
return total/count;
}
private:
double total;
size_t count;
};
class Average2
{
public:
Average2() : av( 0 ), count( 0 ) {}
void add( double value )
{
av = (av*count + value)/(count+1);
count++;
}
double average()
{
return av;
}
private: …Run Code Online (Sandbox Code Playgroud) 我需要从字节数组(std :: vector)中提取内容到bitsets.内容可能跨越两个字节.
这是我的单元测试:
std::vector<uint8_t> val = { 0xAB, 0xCD, 0xEF }; // is 101010111100110111101111
std::bitset<4> a = extractToBitSet<4>( val, 0 ); // should be 0x0A: 1010
std::bitset<8> bc = extractToBitSet<8>( val, 4 ); // should be 0xBC: 10111100
std::bitset<12> def = extractToBitSet<12>( val, 12 ); // should be 0x0DEF: 110111101111
CPPUNIT_ASSERT( a.to_string() == "1010" );
CPPUNIT_ASSERT( bc.to_string() == "10111100" );
CPPUNIT_ASSERT( def.to_string() == "110111101111" );
unsigned long aVal = a.to_ulong();
unsigned long bcVal = bc.to_ulong();
unsigned long defVal = …Run Code Online (Sandbox Code Playgroud) 考虑这个非常简单的代码:
#include <memory>
class Foo
{
public:
Foo() {};
};
class Bar
{
public:
Bar( const std::shared_ptr<Foo>& foo ) {}
};
int main()
{
Foo* foo = new Foo;
Bar bar( std::shared_ptr<Foo>( foo ) );
return 0;
}
Run Code Online (Sandbox Code Playgroud)
为什么Visual Studio会报告
warning C4930: 'Bar bar(std::shared_ptr<Foo>)': prototyped function not called (was a variable definition intended?)
Run Code Online (Sandbox Code Playgroud)
并且没有bar创建对象......如何将此行Bar bar( std::shared_ptr<Foo>( foo ) );解释为函数定义?
我检查了类型名称与new之后的括号吗?还有C++:警告:C4930:未调用原型函数(是一个变量定义?),但我觉得我的问题不同,因为我没有使用语法Foo()也没有Bar().
编辑:请注意,它成功编译:
Foo* foo = new Foo;
std::shared_ptr<Foo> fooPtr( …Run Code Online (Sandbox Code Playgroud) 我在 iOS 上部署动态共享库时遇到了困难。
\n\n为了隔离和暴露问题,我有一个非常简单的“HelloWorld”项目:一个库导出类,其中包含返回“Hello World”的函数,以及一个使用该类并显示消息的程序。
\n\n我正在使用 QtCreator 和 Qt 5.5。
\n\n我能够生成文件.dylib并链接我的程序。但是,当我将其部署到 iPhone 上时,出现错误:
D\xc3\xa9marrage des processus distants.\ndyld: Library not loaded: libMyLib.1.dylib\n Referenced from: /private/var/mobile/Containers/Bundle/Application/D6942CCE-828D-4C10-86DA-F7DA7ADF7449/MyApp.app/MyApp\n Reason: image not found \nRun Code Online (Sandbox Code Playgroud)\n\n在 Android 上,我遇到了同样的问题,可以通过使用ANDROID_EXTRA_LIBS. 但我找不到 iOS 的等效项。
这是我的 .pro 文件。完整的项目可以在这里下载。我将此作为一个错误向 Qt报告,但如果有人能提出一种解决方法,这将会有所帮助!
\n\nMyLib.pro:
\n\nQT -= core gui\n\nTARGET = MyLib\nTEMPLATE = lib\n\nDEFINES += MYLIB_LIBRARY\n\nSOURCES += mylib.cpp\n\nCONFIG += shared\n\nHEADERS += mylib.h\\\n mylib_global.h\nRun Code Online (Sandbox Code Playgroud)\n\n我的应用程序.pro:
\n\nQT += core gui\n\ngreaterThan(QT_MAJOR_VERSION, …Run Code Online (Sandbox Code Playgroud) c++ ×8
qt ×3
algorithm ×1
average ×1
boost ×1
cmake ×1
dylib ×1
ffmpeg ×1
ios ×1
memory-leaks ×1
performance ×1
qt-creator ×1
valgrind ×1
video ×1