小编The*_*der的帖子

问:我如何处理用户按"X"(关闭)按钮的事件?

我正在使用Qt开发一个应用程序.我不知道哪个插槽对应于"用户点击窗口框架的'X'(关闭)按钮"事件"即此按钮:

关闭窗口的按钮

如果没有这方面的插槽,任何人都可以建议我一些其他的方法,我可以在用户按下关闭按钮后启动一个功能.

c++ qt

108
推荐指数
4
解决办法
10万
查看次数

为什么std :: bitset <8>变量无法处理11111111?

为什么这个程序显示以下输出?

#include <bitset>
...

{
    std::bitset<8> b1(01100100); std::cout<<b1<<std::endl;
    std::bitset<8> b2(11111111); std::cout<<b2<<std::endl; //see, this variable
                                                           //has been assigned
                                                           //the value 11111111
                                                           //whereas, during
                                                           //execution, it takes
                                                           //the value 11000111
    std::cout << "b1 & b2: " << (b1 & b2) << '\n';
    std::cout << "b1 | b2: " << (b1 | b2) << '\n';
    std::cout << "b1 ^ b2: " << (b1 ^ b2) << '\n';
}
Run Code Online (Sandbox Code Playgroud)

这是输出:

01000000
11000111
b1 & b2: 01000000
b1 | b2: 11000111
b1 ^ b2: 10000111 …
Run Code Online (Sandbox Code Playgroud)

c++ binary bitset

14
推荐指数
2
解决办法
1102
查看次数

书籍为什么说"编译器为内存中的变量分配空间"?

书籍为什么说"编译器为内存中的变量分配空间".这不是可执行文件吗?我的意思是,例如,如果我写下面的程序,

#include <iostream>
using namespace std;

int main()
{
   int foo = 0;
   cout<<foo;
   return 0;
}
Run Code Online (Sandbox Code Playgroud)

现在,如果我运行program.exe,这个可执行文件将自己命令为变量foo分配一些空间,并编译它,并获得一个可执行文件(让它成为program.exe).不是吗?请解释为什么书籍继续说,"编译器会这样做......这样做"而实际上,编译后的可执行文件就是这样做的.

在这个问题中添加另一个相关问题,为什么sizeof称为编译时运算符?它不是一个实际的运行时运算符吗?

c++ compiler-construction runtime sizeof

11
推荐指数
3
解决办法
809
查看次数

std :: rethrow_exception因嵌套线程而失败

我有一个程序需要将异常从任何未被捕获的线程传播到主线程,以便它可以向用户提供有关失败原因和安全关闭的信息.我的InterThreadException处理程序似乎工作,但只有当抛出异常的线程由主线程直接生成时.

InterTheadException处理器在所有情况下都正确地调用,而被发信具有传递给它的异常指针的异常传播,并在主线程接收到它已经收到了新的异常通知,但调用std::rethrow_exception的异常指针只是失败似乎什么也没做.我已经测试过从这两个不同的线程中抛出完全相同的异常,并且无论我抛出什么,问题似乎都会持续存在.

我怀疑我对如何使用异常指针有一个基本的误解,但我不确定.

这是我的实施InterThreadExceptionHandler.

class InterThreadExceptionHandler : NonCopyable
{
public:
    InterThreadExceptionHandler();
    ~InterThreadExceptionHandler();

    //
    // Sends an exception into this handler, informing it that 
    //this exception has been caught
    // and may be propagated to another thread
    //
    void sendException(std::exception_ptr exception);

    //
    // Freezes the calling thread until an exception has been sent,
    //then rethrows said exception
    //
    void waitForException();

private:
    std::vector<std::exception_ptr> sentExceptions; 
    std::mutex sentExceptionsMutex;

    Semaphore sentExceptionsCounter;
};


InterThreadExceptionHandler::InterThreadExceptionHandler()
{
}
InterThreadExceptionHandler::~InterThreadExceptionHandler()
{ …
Run Code Online (Sandbox Code Playgroud)

c++ multithreading exception c++11 visual-studio-2012

7
推荐指数
1
解决办法
578
查看次数

如何用它的值改变QProgressBar文本的颜色?

当它的值接近50%时,我不知道如何在进度条中部分更改文本的颜色.此效果自动出现在融合样式进度条中(下图).有谁知道这是怎么做的?

融合风格进度条

c++ qt progress-bar

7
推荐指数
1
解决办法
4023
查看次数

Windows多处理

正如我所发现的,在进行多处理时,windows有点麻烦,对此我有一个疑问。

pydoc指出,使用多重处理时,您应该保护Windows应用程序的入口点

这是否仅意味着创建新流程的代码?

例如

脚本1

import multiprocessing

def somemethod():
    while True:
        print 'do stuff'

# this will need protecting
p = multiprocessing.Process(target=somemethod).start()

# this wont
if __name__ == '__main__':
    p = multiprocessing.Process(target=somemethod).start()
Run Code Online (Sandbox Code Playgroud)

在此脚本中,您需要将其包装在if main中,因为产生该过程的行。 但是,如果有的话呢?

剧本2

file1.py

import file2
if __name__ == '__main__':
    p = Aclass().start()
Run Code Online (Sandbox Code Playgroud)

file2.py

import multiprocessing
ITEM = 0
def method1():
    print 'method1'

method1()

class Aclass(multiprocessing.Process):
    def __init__(self):
        print 'Aclass'
        super(Aclass, self).__init__()

    def run(self):
        print 'stuff'
Run Code Online (Sandbox Code Playgroud)

在这种情况下需要保护什么? 如果在文件2中有一个if __main__会发生什么,如果正在创建一个进程,该代码中的代码会被执行吗?

注意:我知道代码将无法编译。这只是一个例子。

python windows multiprocessing

2
推荐指数
1
解决办法
905
查看次数

二维数组的字符指针是否需要由两个null终止?

前几天,我正在读一本书,在那里我发现了这段代码:

char *words[][40] = {"Goat", "A herbivore",
                     "Dog", "An omnivore",
                     "Fox", "A carnivore",
                     "Bear", "An omnivore"
                     "", ""};
Run Code Online (Sandbox Code Playgroud)

书中说,"注意,列表必须以两个空值终止".但是当我编译没有null的代码时,它根据需要编译和工作.请解释空值是否有用.我是C(++)的新手,所以请详细说明.

c++

2
推荐指数
1
解决办法
1005
查看次数

解释这个C++函数中的程序流程

我写了一个奇怪的函数来找到一个数字的阶乘

int strange_fact(int n=0)
{
    static int i=n;
    static int j=i;
    if(j>1)     
    {       
        i *= --j;
        strange_fact();         
        return 0x7777;    //<------ This line
    }
    else
        return i;
}
Run Code Online (Sandbox Code Playgroud)

当我评论第9行时,我得到了预期的输出.但是在添加该行后我遇到了一种奇怪的(或者可能不那么奇怪的)行为.当我取消注释它时发生的是程序流到达第9行,即使递归函数调用在它之前.我的问题是,流量如何到达第9行?

c++

2
推荐指数
1
解决办法
212
查看次数

C++ - while(getline)没有得到第一行文件

我一直在研究一个C++程序,在那里我读取文件内容然后复制到另一个文件,但它似乎总是跳过第一行.我见过其他人遇到麻烦,他们使用了这些代码行:

file.clear();
file.seekg(0);
Run Code Online (Sandbox Code Playgroud)

重置位置,但它不适合我.我已经在多个地方尝试过,但仍然没有运气.有任何想法吗?继承我的代码.

ofstream write("Mar 23 2013.txt");

for(int x = 1; x <= 50; x++){

    stringstream ss;
    ss << "MAR23_" << x;

    ifstream file(ss.str().c_str());

    if(!file.is_open())
        cout << ss.str() << " could not be opened/found." << endl;
    else{  

        while(getline(file,line)){

            file >> time >> ch >> sensor1 >> ch >> temp >> ch >> 
                    sensor2 >> ch >> sensor3;

            file.ignore(numeric_limits<streamsize>::max(), '\n');

            //output = convertEpoch(time);

            write << time << "  Temperature:" << temp << "ºF  S1:" <<
                        sensor1 << "  S2:" << …
Run Code Online (Sandbox Code Playgroud)

c++ reset getline ifstream

1
推荐指数
1
解决办法
1万
查看次数

为什么std :: bitset <8>变量无法处理11111111?

为什么这个程序显示以下输出?

#include <bitset>
#include <cstdio>
#include <iostream>

int main()
{
    std::bitset<8> b1(01100100); std::cout<<b1<<std::endl;
    std::bitset<8> b2(11111111); std::cout<<b2<<std::endl; //see, this variable
                                                           //has been assigned
                                                           //the value 11111111
                                                           //whereas, during
                                                           //execution, it takes
                                                           //the value 11000111.
                                                           //Same is the case with b1
    std::cout << "b1 & b2: " << (b1 & b2) << '\n';
    std::cout << "b1 | b2: " << (b1 | b2) << '\n';
    std::cout << "b1 ^ b2: " << (b1 ^ b2) << '\n';
getchar();
return 0;
}
Run Code Online (Sandbox Code Playgroud)

这是输出: …

c++

0
推荐指数
1
解决办法
239
查看次数