• 8084阅读
  • 4回复

QT多线程编程,添加一个线程后,我的背景,图片都不显示了,救助!!!!!! [复制链接]

上一主题 下一主题
离线wandersky
 
只看楼主 倒序阅读 楼主  发表于: 2009-09-07
环境:板子为2410,arm-gcc-3.3.2  QTE2.3.7
以下为mainwindow.cpp
#include "mainwindow.h"
#include    "my_define.h"
#include "serialthread.h"


MyMainWindow :: MyMainWindow()
{    

    fd = open(MODEMDEVICE, O_RDWR | O_NOCTTY );
    if (fd<0)
        printf("open %s fail %d\n",MODEMDEVICE,fd);
    else
        printf("open %s success %d\n",MODEMDEVICE,fd);
    //a = new SerialThread(this);

    setGeometry(0,0,320,240);
    //this->setMinimumSize(320,240);
    //this->setMaximumSize(320,240);
    QPixmap image("background.jpg");
    setBackgroundPixmap(image);   //这个地方没有起作用呀
    
    b1=new QPushButton("verygood",this);
    QPalette    newPalette=b1->palette();
    newPalette.setColor(QColorGroup::ButtonText, Qt::red);
    b1->setPalette(newPalette);    
    b1->setGeometry(30,140,120,40);
    b1->setFont(QFont("Times",18,QFont::Bold));

    b2=new QPushButton("yibangood",this);
    QPalette pal = b2->palette();
    pal.setColor(QColorGroup::Button, Qt::blue);
    b2->setPalette(pal);
        b2->setGeometry(180,140,120,40);
        b2->setFont(QFont("Times",18,QFont::Bold));
    
    b3=new QPushButton("bad1",this);
        b3->setGeometry(30,190,50,40);
        b3->setFont(QFont("Times",18,QFont::Bold));

    b4=new QPushButton("bad2",this);
        b4->setGeometry(100,190,50,40);
        b4->setFont(QFont("Times",18,QFont::Bold));

    b5=new QPushButton("bad3",this);
        b5->setGeometry(180,190,50,40);
        b5->setFont(QFont("Times",18,QFont::Bold));

    b6=new QPushButton("bad4",this);
        b6->setGeometry(250,190,50,40);
        b6->setFont(QFont("Times",18,QFont::Bold));
    
    /*QVBoxLayout    *vbox    =new QVBoxLayout(this);
    QHBoxLayout    *hbox1    =new QHBoxLayout();
    QHBoxLayout    *hbox2    =new QHBoxLayout();

    vbox->addLayout(hbox1);
    vbox->addLayout(hbox2);
    
    hbox1->addWidget(b1);
    hbox1->addWidget(b2);
    hbox2->addWidget(b3);
    hbox2->addWidget(b4);
    hbox2->addWidget(b5);
    hbox2->addWidget(b6);*/

    connect(b1,SIGNAL(clicked()),this,SLOT(sendverygood()));
    connect(b2,SIGNAL(clicked()),this,SLOT(sendgood()));
    connect(b3,SIGNAL(clicked()),this,SLOT(sendbad1()));
    connect(b4,SIGNAL(clicked()),this,SLOT(sendbad2()));
    connect(b5,SIGNAL(clicked()),this,SLOT(sendbad3()));
    connect(b6,SIGNAL(clicked()),this,SLOT(sendbad4()));    


    //label=new QLabel(this);
    //label->setGeometry(10,110,150,50);
    //label->setText("If you click the button above,\n the whole program will exit");


}

void    MyMainWindow::paintEvent(QPaintEvent *)//这个地方也没有起作用呀
{
    QPixmap    image("zhiye.jpg");
    QPainter    paint;
    paint.begin(this);
    paint.drawPixmap(20,13,image);
    paint.end();
}

void    playxiexie()
{
    QSound bells("/test/xiexie.wav");  
    bells.play();
}

void    playnihao()
{
    QSound bells("/test/nihao.wav");  
    bells.play();
}

void    playfuwu()
{
    QSound bells("/test/fuwu.wav");  
    bells.play();
}


void    MyMainWindow::sendverygood()
{
    char buf[]={'v','e','r','y','g','o','o','d',CHANGE_LINE};            
    write(fd,buf,sizeof(buf));
    QSound bells("/test/xiexie.wav");  
    bells.play();
    //a->start();
    //a->wait();
}
        void    MyMainWindow::sendgood()
{
    char buf[]={'y','i','b','a','n','g','o','o','d',CHANGE_LINE};  
        
    write(fd,buf,sizeof(buf));
    playxiexie();
    //a->stop();
    /*if(QSound::available()== true)
    {
        //printf("it's available\n");
        QSound bells("/test/xiexie.wav");  
        bells.play();
    }
    else
    {
        //printf("not available\n");
    }
    system("madplay /test/xingkong.mp3");*/    
}

    void    MyMainWindow::sendbad1()
{  
    char buf[]={'b','a','d','1',CHANGE_LINE};            
    write(fd,buf,sizeof(buf));
    playxiexie();
}

        void    MyMainWindow::sendbad2()
{
    char buf[]={'b','a','d','2',CHANGE_LINE};            
    write(fd,buf,sizeof(buf));
    playxiexie();
}
        void    MyMainWindow::sendbad3()
{

    char buf[]={'b','a','d','3',CHANGE_LINE};            
    write(fd,buf,sizeof(buf));
    playxiexie();
}

        void    MyMainWindow::sendbad4()
{
    char buf[]={'b','a','d','4',CHANGE_LINE};            
    write(fd,buf,sizeof(buf));
    playxiexie();
}
以下为"mainwindow.h"
#ifndef __MAIN_WINDOW_H
#define __MAIN_WINDOW_H

#include <qwidget.h>
#include <qsound.h>
#include <qpushbutton.h>
#include <qfont.h>
#include <qlabel.h>
#include <qlcdnumber.h>
#include <qslider.h>
#include <qpainter.h>
#include <qpixmap.h>
#include <qlayout.h>

//class SerialThread;

class MyMainWindow : public QWidget
{
Q_OBJECT
public:
    MyMainWindow();
    ~MyMainWindow(){};
    int        fd;
public slots:  
    void sendverygood();
    void sendgood();
    void sendbad1();
    void sendbad2();
    void sendbad3();
    void sendbad4();
    //void loadJPEGFile();
  
protected:
    void    paintEvent(QPaintEvent *);
private:
    
    QLabel        *name;
    QLabel        *num;    
    QPushButton    *b1;
    QPushButton    *b2;
    QPushButton    *b3;
    QPushButton    *b4;
    QPushButton    *b5;
    QPushButton    *b6;    
    //SerialThread     *a;
    
};

#endif
[ 此帖被wandersky在2009-09-07 18:06重新编辑 ]
离线wandersky
只看该作者 1楼 发表于: 2009-09-07
以下为main.cpp
#include <qapplication.h>
#include <qwidget.h>
#include <qpushbutton.h>
#include <qfont.h>
#include <qlabel.h>    
#include <qscrollview.h>
#include <qlcdnumber.h>
#include <qslider.h>
#include "mainwindow.h"
int main(int argc,char ** argv)
{
    QApplication a(argc,argv);
    MyMainWindow w;
    a.setMainWidget(&w);
    w.show();
    a.exec();
}
以下为serialthread.cpp
#include "my_define.h"
#include "serialthread.h"
#include "mainwindow.h"

#define _POSIX_SOURCE 1 /* POSIX compliant source */

volatile int STOP=FALSE;
int wait_flag=TRUE; /* TRUE while no signal received */


SerialThread::SerialThread(MyMainWindow *parent){
    this->parent = parent;
}


void SerialThread::run()
{

    int res;
    struct termios oldtio,newtio;
    char buf[255];

    tcgetattr(parent->fd,&oldtio);
    bzero(&newtio, sizeof(newtio)); /* clear struct for new port settings */

/*
BAUDRATE: Set bps rate. You could also use cfsetispeed and cfsetospeed.
CRTSCTS : output hardware flow control (only used if the cable has
all necessary lines. See sect. 7 of Serial−HOWTO)
CS8 : 8n1 (8bit,no parity,1 stopbit)
CLOCAL : local connection, no modem contol
CREAD : enable receiving characters
*/

    newtio.c_cflag = BAUDRATE | CRTSCTS | CS8 | CLOCAL | CREAD;

/*
IGNPAR : ignore bytes with parity errors
ICRNL : map CR to NL (otherwise a CR input on the other computer
will not terminate input)
otherwise make device raw (no other input processing)
*/

    newtio.c_iflag = IGNPAR | ICRNL;

/*
Raw output.
*/

    newtio.c_oflag = 0;

/*
ICANON : enable canonical input
disable all echo functionality, and don't send signals to calling program
*/

    newtio.c_lflag = ICANON;

/*
initialize all control characters
default values can be found in /usr/include/termios.h, and are given
in the comments, but we don't need them here
*/

newtio.c_cc[VINTR] = 0; /* Ctrl−c */
newtio.c_cc[VQUIT] = 0; /* Ctrl−\ */
newtio.c_cc[VERASE] = 0; /* del */
newtio.c_cc[VKILL] = 0; /* @ */
newtio.c_cc[VEOF] = 4; /* Ctrl−d */
newtio.c_cc[VTIME] = 0; /* inter−character timer unused */
newtio.c_cc[VMIN] = 1; /* blocking read until 1 character arrives */
newtio.c_cc[VSWTC] = 0; /* '\0' */
newtio.c_cc[VSTART] = 0; /* Ctrl−q */
newtio.c_cc[VSTOP] = 0; /* Ctrl−s */
newtio.c_cc[VSUSP] = 0; /* Ctrl−z */
newtio.c_cc[VEOL] = 0; /* '\0' */
newtio.c_cc[VREPRINT] = 0; /* Ctrl−r */
newtio.c_cc[VDISCARD] = 0; /* Ctrl−u */
newtio.c_cc[VWERASE] = 0; /* Ctrl−w */
newtio.c_cc[VLNEXT] = 0; /* Ctrl−v */
newtio.c_cc[VEOL2] = 0; /* '\0' */
/*
now clean the modem line and activate the settings for the port
*/
    tcflush(parent->fd, TCIFLUSH);
    tcsetattr(parent->fd,TCSANOW,&newtio);
/*
terminal settings done, now handle input
In this example, inputting a 'z' at the beginning of a line will
exit the program.
*/

    while (STOP==FALSE) {
        printf("thread run\n");
        res = read(parent->fd,buf,255);
        buf[res]=0; /* set end of string, so we can printf */
        printf(":%s:%d\n", buf, res);
        if (buf[0]=='z')
        STOP=TRUE;
    }
    /* restore the old port settings */
    tcsetattr(parent->fd,TCSANOW,&oldtio);

    /*int    fd;
    char buf[]={'h','e','l','l','o',CHANGE_LINE};  
    fd = open("/dev/ttyS1", O_RDWR | O_NOCTTY );  
    if (fd<0)
        printf("open %s fail %d\n",MODEMDEVICE,fd);
    else
        printf("open %s success %d\n",MODEMDEVICE,fd);
    STOP=FALSE;
    while (STOP==FALSE)    
    {          
        write(fd,buf,sizeof(buf));
    }
    STOP=TRUE;*/
}
/***************************************************************************
* signal handler. sets wait_flag to FALSE, to indicate above loop that *
* characters have been received. *
***************************************************************************/

void SerialThread::stop()
{
    STOP=TRUE;
}
以下为serialthread.h
#ifndef SERIAL_THREAD_H
#define  SERIAL_THREAD_H
#include    <qthread.h>
#include <termios.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/signal.h>
#include <sys/types.h>

#include    "mainwindow.h"

class MyMainWindow;

class SerialThread: public QThread {
public:
    SerialThread(MyMainWindow *parent);
    virtual void run();
    void stop();
private:
    MyMainWindow *parent;
};
#endif
离线wandersky
只看该作者 2楼 发表于: 2009-09-07
请高人指点!!!!!!!!
详细说明:当我把这两个文件"serialthread.h","serialthread.cpp文件删除之后,然后再把程序中有关"线程的变量删除,重新编译,我设置的背景图片就能够显示出来 ,我画的图片也能够显示出来,但是添加上"serialthread.h,"serialthread.cpp这两个文件之后就不能显示了,只能显示我的按钮。
[ 此帖被wandersky在2009-09-07 18:47重新编辑 ]
离线speaker424
只看该作者 3楼 发表于: 2009-09-08
我也遇到同样的问题,好像新的线程阻止了画面的显示
离线wandersky
只看该作者 4楼 发表于: 2009-09-08
期待高手,给点提示,
上楼解决了吗??
快速回复
限100 字节
 
上一个 下一个