Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. International
  3. Chinese
  4. 碰到个好奇怪的问题
Forum Updated to NodeBB v4.3 + New Features

碰到个好奇怪的问题

Scheduled Pinned Locked Moved Chinese
7 Posts 2 Posters 4.5k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    eiao
    wrote on last edited by
    #1

    我在widget构造函数里运行了循环刷新界面的函数
    void go()
    {
    while(on)
    {
    ...
    repaint();
    QApplication::processevent();
    }
    }
    然后在closeevent里边这样写
    on=false;
    运行程序,界面打开~按下alt+F4
    运行到了closeevent,并且widget关闭,但是Qt界面显示程序仍在运行,要按下那个红方块才行,但是会显示异常退出。

    如果在构造函数里没有go();程序能正常退出。
    如果在main里边加一句...
    widget w;
    w.show();//加上的
    窗口在关闭后能再次打开,此时按下alt+F4,能正常关闭!
    希望能得到帮助,以及查找问题的步骤!

    然后又手贱试了下...
    如果在widget中加入
    this->deletelater
    会发现析构函数被执行了两次...
    一次实在widget里,一次在main里边
    不应该每次执行析构都表明一个实例被释放了么...
    那么执行两次说明有两个?!
    万分感激!

    1 Reply Last reply
    0
    • J Offline
      J Offline
      jiangcaiyang
      wrote on last edited by
      #2

      建议使用QTimer来刷新界面,使用死循环这种方法实在是不好,会耗CPU到50%的!
      使用QTimer的方法很简单:
      @QTimer timer;
      timer.setInterval( 16 );// 大概60次/秒
      connect( &timer, SIGNAL( timeout( ) ), this, update( ) );@

      1 Reply Last reply
      0
      • E Offline
        E Offline
        eiao
        wrote on last edited by
        #3

        [quote author="jiangcaiyang" date="1375160837"]建议使用QTimer来刷新界面,使用死循环这种方法实在是不好,会耗CPU到50%的!
        使用QTimer的方法很简单:
        @QTimer timer;
        timer.setInterval( 16 );// 大概60次/秒
        connect( &timer, SIGNAL( timeout( ) ), this, update( ) );@[/quote]

        QTimer精度低...我想精度在2ms以内..

        1 Reply Last reply
        0
        • J Offline
          J Offline
          jiangcaiyang
          wrote on last edited by
          #4

          如果是两毫秒内的话,那么还是使用循环吧,但是不建议你那么写。
          可以这样:
          @while ( loop )
          {
          loop = CheckIfQuit( );
          Render( );
          }

          bool CheckIfQuit( void )
          {
          if ( XButtonIsPressed( ) ) return false;
          else return true;
          }@

          1 Reply Last reply
          0
          • E Offline
            E Offline
            eiao
            wrote on last edited by
            #5

            [quote author="jiangcaiyang" date="1375182647"]如果是两毫秒内的话,那么还是使用循环吧,但是不建议你那么写。
            可以这样:
            @while ( loop )
            {
            loop = CheckIfQuit( );
            Render( );
            }

            bool CheckIfQuit( void )
            {
            if ( XButtonIsPressed( ) ) return false;
            else return true;
            }@[/quote]

            真是非常好的主意,从没有想到,不过...怎么在Qt不通过事件循环直接渲染...另外还要收网络命令...我再研究研究...

            1 Reply Last reply
            0
            • J Offline
              J Offline
              jiangcaiyang
              wrote on last edited by
              #6

              在游戏中一般是这么写的。
              但是呢,我昨晚想了一下,我以前在使用QWidget的时候,还可以这么写。
              @Widget::paintEvent( QPaintEvent* pEvent )
              {
              DoSomeRendering( );
              repaint( );// 或者update( )如果你需要来自队列的渲染指令的话
              }@
              这样毫不影响对X按钮关闭的操作。

              1 Reply Last reply
              0
              • E Offline
                E Offline
                eiao
                wrote on last edited by
                #7

                [quote author="jiangcaiyang" date="1375233704"]在游戏中一般是这么写的。
                但是呢,我昨晚想了一下,我以前在使用QWidget的时候,还可以这么写。
                @Widget::paintEvent( QPaintEvent* pEvent )
                {
                DoSomeRendering( );
                repaint( );// 或者update( )如果你需要来自队列的渲染指令的话
                }@
                这样毫不影响对X按钮关闭的操作。[/quote]

                的确啊~原来还以为repaint也是经过队列的~一直搞混了,多谢!准备为事件做个过滤,一旦开始刷新,只接受自己需要的事件...

                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved