모든 대화를 저장하기 위해 SQLite 데이터베이스를 사용하는 작은 대화 응용 프로그램이 있습니다. 나는 앱이 무작위로 얼어 붙는 것을 알아 차렸다. 그런 다음 앱을 다시 작동 시키려면 최소화하고 최대화해야한다. 문제는 GUI가 멈추는 원인이되는 SQLite select/inserts 일 수 있다고 생각했습니다. 모든 SQLite 메서드를 별도의 스레드로 옮기려고했습니다.별도의 스레드에서 실행중인 Im 임에도 불구하고 QT GUI가 작동하지 않습니다.
이렇게하면 앱이 계속 정지됩니다.
가치가있을 수도 있습니다 어떤 것들은 알고 :
내가 내
MainWindow
에서 직접QTcpSocket
를 사용하지만 별도의 스레드에서QTcpSocket
를 실행에 아무 소용이없는 것 같다을?나는 새 스레드에 SQLite는 방법을 분리 한 내 채팅 메시지를 표시하는 3
WebViews
를 사용(아래 구현을 참조), 전체 응용 프로그램의 GUI는이
WebViews
아래 코드는 실제로 별도의 스레드에서 실행됩니까? GUI가 여전히 고정되어 있습니다.
내 헤더 파일 :class dbThread : public QObject
{
Q_OBJECT
public:
dbThread(QObject* parent);
public slots:
bool openDB(QString agentID);
signals:
void clearPreviousHistory();
private:
QSqlDatabase db;
QHash<QString, QString> countries;
};
내 CPP 파일 :
dbtrad->getHistory(channelId);
편집
: 내MainWindow
에서
dbThread
메소드를 호출하는 방법을
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
QThread* thread = new QThread(this);
dbtrad = new dbThread(this);
dbtrad->moveToThread(thread);
dbtrad->openDB(userID);
connect(dbtrad, SIGNAL(clearPreviousHistory()), this, SLOT(clearHistoryV()));
thread->start();
}
dbThread::dbThread(QObject * parent): QObject(parent) {
}
bool dbThread::openDB(QString agentID) {
qDebug() << "OPEN DB FROM THREAD ";
// Find QSLite driver
db = QSqlDatabase::addDatabase("QSQLITE");
// ......
}
이것은
새로운 코드 :
// Start database thread
QThread* thread = new QThread(this);
dbtrad = new dbThread(this);
dbtrad->moveToThread(thread);
connect(this, SIGNAL(requestOpenDB(QString)), dbtrad, SLOT(openDB(QString)));
thread->start();
emit requestOpenDB(userID);
여기서 dbthread 함수를 호출하는 코드를 보여줍니다. 또한 메인 스레드에서 호출합니까? 'moveToThread'가하는 일을 오해 할 수도 있습니다. –
편집을 참조하십시오. 메인 스레드에서 db 메소드를 호출하는 방법을 게시했습니다. – Alosyius
[QThread :: currentThreadId() ** 경고 : **이 함수에서 반환하는 핸들은 내부 용으로 사용되었으므로 응용 프로그램 코드. ** 경고 : ** Windows에서 반환 값은 현재 스레드의 의사 핸들입니다.] (http://qt-project.org/doc/qt-4.8/qthread.html#currentThreadId) – UmNyobe