C++ 调用qwidget::repaint()时崩溃
我们正在尝试编写一个简单的messenger,但遇到了一个问题。 当我们从另一个线程调用repaint时,程序崩溃。 这是我们代码的一部分 `C++ 调用qwidget::repaint()时崩溃,c++,qt,C++,Qt,我们正在尝试编写一个简单的messenger,但遇到了一个问题。 当我们从另一个线程调用repaint时,程序崩溃。 这是我们代码的一部分 ` 我不是一个qt专家,但看起来qt不想让你在GUI线程之外乱开窗口(重新绘制)。这对我来说似乎是合理的,因为否则窗口代码中必须有大量的锁才能安全工作。难道主GUI线程不应该自己重新绘制吗?它会崩溃,因为您不能这样做。在qt.nokia.com上有关于这方面的好资料。如果你不阅读文档,就会发生这种情况!我们需要在调用client::contacts时重新绘制
我不是一个qt专家,但看起来qt不想让你在GUI线程之外乱开窗口(重新绘制)。这对我来说似乎是合理的,因为否则窗口代码中必须有大量的锁才能安全工作。难道主GUI线程不应该自己重新绘制吗?它会崩溃,因为您不能这样做。在qt.nokia.com上有关于这方面的好资料。如果你不阅读文档,就会发生这种情况!我们需要在调用client::contacts时重新绘制窗口。我们应该怎样做呢?因此包含了很多关于线程和qt的问题。例如,昨天的这个问题也有一个答案:
//client.cpp
#include "main_window.h"
extern main_window * m_parent; //m_parent in main_window constructor get assigned with this
std::list<std::string> m_online_contacts;
void client::contacts(std::string str)
{
m_online_contacts.clear();
std::string user_n = "";
size_t j = 0;
size_t size = str.size();
j = str.find(':');
if (size > 2){
str = str.substr(j + 1);
j = str.find(':');
while(j != std::string::npos){
user_n = str.substr(0, j);
m_online_contacts.push_back(user_n);
str = str.substr(j + 1);
j = str.find(':');
}
m_online_contacts.push_back(str);
}
if(m_parent)
m_parent->create_contacts(m_online_contacts);
}
//main_window.cpp
void main_window::create_contacts(std::list< std::string> l)
{
for(int j = 0; j < m_count; ++j){ //m_count is a count of on-line users
if(m_users[j]) { //m_users[] is a list of users to be shown
delete m_users[j];
}
}
if(m_users) {
delete [] m_users;
}
m_count = 0;
std::list <std::string> :: iterator it = l.begin();
if(l.size() == 0) {
m_users = new m_label* [1];
m_users[0] = new m_label(QString::fromStdString("No online contacts"));
m_layout->addWidget(m_users[0]);
m_count = 1;
}
else {
m_users = new m_label * [l.size()];
for (int i = 0; it != l.end(); ++it) {
m_users[i] = new m_label (QString::fromStdString(*it));
m_layout->addWidget(m_users[i++]);
++m_count;
}
}
this->repaint();
}
QObject::setParent: Cannot set parent, new parent is in a different thread
QObject::setParent: Cannot set parent, new parent is in a different thread
QPixmap: It is not safe to use pixmaps outside the GUI thread
QPixmap: It is not safe to use pixmaps outside the GUI thread
Segmentation fault