我使用QLabel向用户显示更大,动态变化的QPixmap的内容.根据可用空间,使这个标签更小/更大会很好.屏幕尺寸并不总是与QPixmap一样大.
如何修改QLabel QSizePolicy和sizeHint()QLabel以调整QPixmap的大小,同时保持原始QPixmap的宽高比?
我无法修改sizeHint()QLabel,设置minimumSize()为零无济于事.hasScaledContents()QLabel上的设置允许增长,但是打破了宽高比...
子类化QLabel确实有帮助,但是这个解决方案只为一个简单的问题添加了太多的代码......
任何聪明的提示如何在没有子类化的情况下完成此任务?
为什么以下代码在Visual Studio和GCC上都崩溃了?
要使它崩溃,需要基于范围的for循环,std :: map,std :: string并引用字符串.如果我删除其中任何一个它将工作.
#include <iostream>
#include <string>
#include <map>
using namespace std;
struct S
{
map<string, string> m;
S()
{
m["key"] = "b";
}
const string &func() const
{
return m.find("key")->second;
}
};
int main()
{
for (char c : S().func())
cout << c;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
Ideone链接:http://ideone.com/IBmhDH
如果我有一个A相等的字符串,"abc"并且我想使用字符串B的相反形式的字符串A,为什么我reverse_copy()不能这样做呢?
std::string A = "abc";
std::string B;
std::reverse_copy(A.begin(), A.end(), B.begin());
std::cout << B << std::endl; // no output
Run Code Online (Sandbox Code Playgroud)
是reverse_copy()用绳子可用?reverse()似乎有效。
我试图解决C ++中的编码问题,该问题计算素数的数量小于非负数的数量n。
所以我首先想出了一些代码:
int countPrimes(int n) {
vector<bool> flag(n+1,1);
for(int i =2;i<n;i++)
{
if(flag[i]==1)
for(long j=i;i*j<n;j++)
flag[i*j]=0;
}
int result=0;
for(int i =2;i<n;i++)
result+=flag[i];
return result;
}
Run Code Online (Sandbox Code Playgroud)
这需要88毫秒,并使用8.6 MB的内存。然后,我将代码更改为:
int countPrimes(int n) {
// vector<bool> flag(n+1,1);
bool flag[n+1] ;
fill(flag,flag+n+1,true);
for(int i =2;i<n;i++)
{
if(flag[i]==1)
for(long j=i;i*j<n;j++)
flag[i*j]=0;
}
int result=0;
for(int i =2;i<n;i++)
result+=flag[i];
return result;
}
Run Code Online (Sandbox Code Playgroud)
这需要28毫秒和9.9 MB。我真的不明白为什么在运行时间和内存消耗上都存在这样的性能差距。我已阅读相类似的问题这一个和那一个,但我仍然困惑。
编辑:我的运行时间与11.5 MB的存储器替换之后减少至40毫秒vector<bool>与vector<char>。
所以当我遇到这个时,我只是在浏览库的源代码.
Font::Font(const sf::Font& font) :
m_font{std::make_shared<sf::Font>(font)}
{
}
Run Code Online (Sandbox Code Playgroud)
我不懂语法
m_font{..}
Run Code Online (Sandbox Code Playgroud)
它是什么?它有什么作用.如果这是一个非常愚蠢的问题,我很抱歉.我不知道谷歌是什么,所以问这里.
在g ++ 4.9.2和5.3.1上,此代码需要几秒钟的时间来编译并生成52,776字节的可执行文件:
#include <array>
#include <iostream>
int main()
{
constexpr std::size_t size = 4096;
struct S
{
float f;
S() : f(0.0f) {}
};
std::array<S, size> a = {}; // <-- note aggregate initialization
for (auto& e : a)
std::cerr << e.f;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
增加size似乎线性增加编译时间和可执行文件大小.我无法使用clang 3.5或Visual C++ 2015重现此行为.使用-Os没有区别.
$ time g++ -O2 -std=c++11 test.cpp
real 0m4.178s
user 0m4.060s
sys 0m0.068s
Run Code Online (Sandbox Code Playgroud)
检查汇编代码显示初始化a已展开,生成4096 movl条指令:
main:
.LFB1313:
.cfi_startproc
pushq %rbx
.cfi_def_cfa_offset 16
.cfi_offset …Run Code Online (Sandbox Code Playgroud) 码:
onDragEnd = {
(event) => this.setState({ playerMarkerPositionFuture: event.nativeEvent.coordinate })
.then(() => alert("hello"))
}
Run Code Online (Sandbox Code Playgroud)
当执行以下代码时,我收到此错误:
undefined is not an object evaluating
('_this.setState({playerMarkerPositionFuture: event.nativeEvent.coordinate}).then')
Run Code Online (Sandbox Code Playgroud)
如果我删除了承诺,那么一切都会按预期进行,因此这意味着我很可能以错误的方式使用了承诺:
onDragEnd={
(event) => this.setState({ playerMarkerPositionFuture: event.nativeEvent.coordinate })
}
Run Code Online (Sandbox Code Playgroud) 关于关键字new与&特定上下文之间的区别,我有一些问题.
让我们说这是我的代码:
struct Base {};
struct Foo : Base {};
struct Storage
{
void save(Base * object) {}
Base * content;
};
int main()
{
Storage s1, s2;
s1.save(new Foo());
s2.save(&Foo());
}
Run Code Online (Sandbox Code Playgroud)
执行main之后,s1将持有指向类型对象的指针Foo.然而,s2将保持指向类型对象的指针Base.在save方法完成执行之前,它s2.content只会指向一个类型的对象Foo.
如果我错了,请纠正我:
据我所知,new Foo()创建一个指向新类型对象的指针Foo.&Foo()另一方面,首先创建一个类型的新对象,Foo然后指向它.
究竟是什么区别new Foo()和&Foo()呢?显然,它们都会为您提供指向现有类型对象的指针Foo.
为什么new Foo()在执行save方法后通过persist 创建对象,而通过创建的对象&Foo()却没有?
可能是&Foo()创建一个临时对象,在执行save之后它将不复存在?如果是的话,我怎样才能延长通过&Foo()它创建的对象的生命(至少)直到毁灭 …
考虑下一个代码示例:
template <typename... TArgs>
void foo(std::function<void(TArgs...)> f) {
}
template <typename... TArgs>
class Class {
public:
static void foo(std::function<void(TArgs...)> f) {
}
};
Run Code Online (Sandbox Code Playgroud)
为什么我可以这样做:
int main() {
// Helper class call
Class<int, int>::foo(
[](int a, int b) {}
);
}
Run Code Online (Sandbox Code Playgroud)
但这样做时出现编译错误:
int main() {
// Function call
foo<int, int>(
[](int a, int b) {}
);
}
Run Code Online (Sandbox Code Playgroud)
<source>:16:5: error: no matching function for call to 'foo'
foo<int, int>(
^~~~~~~~~~~~~
<source>:4:6: note: candidate template ignored: could not match
'std::function<void (int, int, TArgs...)>' …Run Code Online (Sandbox Code Playgroud) 在C++中是否有任何可接受的方法来区分对不可变对象的const引用与可变对象的const引用?
例如
class DataBuffer {
// ...
};
class Params {
// ...
};
class C {
public:
// Given references must be valid during instance lifetime.
C(const Params& immutableParameters, const DataBuffer& mutableDataBuffer) :
m_immutableParameters{immutableParameters},
m_mutableDataBuffer{mutableDataBuffer}
{
}
void processBuffer();
private:
const Params& m_immutableParameters;
const DataBuffer& m_mutableDataBuffer;
};
Run Code Online (Sandbox Code Playgroud)
这里的语义差异仅在名称中给出.
问题是const&实例变量只让您知道该实例不会修改该对象.界面中没有区别是否可以在其他地方修改它们,我认为这是一个有用的功能,能够在界面中进行描述.
通过类型系统表达这一点将有助于使接口更清晰,允许编译器捕获错误(例如C,在上面的示例中意外修改传递给实例的参数,在实例之外),并且可能有助于编译器优化.
假设答案是在C++中无法区分,可能有些东西可以通过一些模板魔术来实现吗?
c++ ×9
arrays ×1
c++11 ×1
function ×1
g++ ×1
javascript ×1
lambda ×1
new-operator ×1
optimization ×1
performance ×1
qlabel ×1
qt ×1
qt4 ×1
react-native ×1
reference ×1
std ×1
stdarray ×1
templates ×1
vector ×1