• 5398阅读
  • 1回复

求助:为什么加上Q_OBJECT宏之后就会报错: [复制链接]

上一主题 下一主题
离线wangyiqing
 
只看楼主 倒序阅读 楼主  发表于: 2007-03-07
代码如下:
channel_select_page.h:
#ifndef __CHANNEL_SELECT_PAGE_H__
#define __CHANNEL_SELECT_PAGE_H__

#include <QWidget>
//#include "gui_event_button.cpp"
//#include <QMenu>
//#include <QLabel>
class Event_button;
class QMenu;
class QLabel;

#define LOGO_WIDTH 136
#define LOGO_HEIGHT 24


class Ch_select_page : public QWidget
{
//   Q_OBJECT
public:
  Ch_select_page(QWidget *parent = 0);
  void set_background(const char *file_path);
  void set_logo(const char *file_path);
public slots:
  void menu_show(bool toggled);
private:
  QLabel *logo;
  Event_button *menu_button;
  QMenu *start;
  void create_menu();
};

#endif
channel_select_page.cpp:
#include "channel_select_page.h"
#include "gui_event_button.h"
#include <QtGui>

//#include <iostream>

//using namespace std;

Ch_select_page::Ch_select_page( QWidget *parent )
  :QWidget(parent)
{
  set_background("./ch_select_bg.png");
  set_logo("./logo.png");
  create_menu();
  resize(240,320);
}
void Ch_select_page::set_background( const char *file_path )
{
  QPalette palette;
  QPixmap bg(file_path);
  palette.setBrush(backgroundRole(),QBrush(bg));
  setPalette(palette);
  setAutoFillBackground(true);
  resize(240,320);
}
void Ch_select_page::set_logo( const char *file_path )
{
  logo = new QLabel(this);
  logo->setPixmap(QPixmap(file_path));
  logo->setGeometry(QRect(0,0,LOGO_WIDTH,LOGO_HEIGHT));
}
void Ch_select_page::menu_show(bool toggled)
{
  if(toggled)
  {
    start->show();
  }
  else
    start->hide();
}
void Ch_select_page::create_menu()
{
  menu_button = new Event_button(QObject::tr("./start.png"),
              QObject::tr("./start_onclick.png"),
              this);
  menu_button->setGeometry(QRect(0,296,57,19));
  menu_button->set_size(62,26);
  menu_button->setIconSize(QSize(62,26));
  start = new QMenu(this);
  start->addMenu(QObject::tr("Web Browser"));
  start->exec(this->mapToGlobal(QPoint(0,280)));
  start->hide();

  connect(menu_button,SIGNAL(toggled(bool)),this,SLOT(menu_show(bool)));
 

}
gui_event_button.h:
#ifndef __GUI_EVENT_BUTTON_H__
#define __GUI_EVENT_BUTTON_H__

#include <QToolButton>
#include <QWidget>

class QHBoxLayout;
class QString;
class QIcon;
class Event_button;

class Event_button : public QToolButton
{
  Q_OBJECT
public:
  Event_button(   QString first_icon_name = NULL,
        QString second_icon_name = NULL,
        QWidget *parent = NULL );
  ~Event_button();
  void set_size( const u_int width,const u_int height );
  void set_first_icon( const QString &first_icon_name );
  void set_second_icon( const QString &second_icon_name );
public slots:
  bool release_handle( bool checked );
  bool press_handle( bool checked );
private:
  QIcon *first_icon;
  QIcon *second_icon;
  QString first_filename;
  QString second_filename;
};

class Tool_bar : public QWidget
{
public:
  Tool_bar( QWidget *parent = 0 );
  Event_button *channel;
  Event_button *msg;
  Event_button *chat;
  Event_button *full_screen;
  //some work should to finish to connect some action to the button
private:
  QHBoxLayout *tool_bar;  
};

#endif
gui_event_button.cpp:
#include "gui_event_button.h"
#include <iostream>
#include <QString>
#include <QIcon>
#include <QObject>
#include <QHBoxLayout>


Event_button::Event_button( QString first_icon_name,QString second_icon_name,QWidget *parent )
  :QToolButton( parent )
{
  setCheckable(true);
  if ( !first_icon_name.isEmpty() )
  {
    std::cout<<"first_icon set first!"<<std::endl;
    first_icon = new QIcon( first_icon_name );
    first_filename = "";
    first_filename += first_icon_name;
  }
  else
  {
    std::cout<<"NULLNULLNULLNULL"<<std::endl;
    first_icon = NULL;
    first_filename = "";
  }
  if ( !second_icon_name.isEmpty() )
  {
    std::cout<<"second icon set first"<<std::endl;
    second_icon = new QIcon( second_icon_name );
    second_filename = "";
    second_filename += second_icon_name;
  }
  else
  {
    std::cout<<"NULLNULLNULLNULL"<<std::endl;
    second_icon = NULL;
    second_filename = "";
  }
  connect( this,SIGNAL( toggled(bool) ),this,SLOT( release_handle(bool) ) );
  connect( this,SIGNAL( toggled(bool) ),this,SLOT( press_handle(bool) ) );
  setIcon( *first_icon );
}

Event_button::~Event_button()
{
  delete first_icon;
  delete second_icon;
}

bool Event_button::release_handle( bool checked )
{
  if ( !checked )
  {
    std::cout<<"release handle is invoked!"<<std::endl;
    if ( this->first_filename.isEmpty() )
    {
        std::cout<<"first_icon is NULL"<<std::endl;
        return false;
    }
    else
    {
        std::cout<<"first_icon reset"<<std::endl;
        if ( first_icon != NULL )
          delete first_icon;
        first_icon = new QIcon( first_filename );
        setIcon( *first_icon );
    }
  }  
  return true;

}


bool Event_button::press_handle( bool checked )
{
 

  if ( checked )
  {
    std::cout<<"press handle is invoked!"<<std::endl;
    if ( this->second_filename.isEmpty() )
    {
        std::cout<<"second_icon is NULL"<<std::endl;
        return false;
    }
    else
    {
        std::cout<<"reset the icon"<<std::endl;
        if ( second_icon != NULL )
          delete second_icon;
        second_icon = new QIcon( second_filename );
        setIcon( *second_icon );
    }
  }
  return true;
}

void Event_button::set_size( const u_int width,const u_int height )
{
  resize(width,height);
}

void Event_button::set_first_icon( const QString &first_icon_name )
{
  if ( !first_icon_name.isEmpty() )
  {
    delete first_icon;
    first_icon = new QIcon( first_icon_name );
    first_filename = first_icon_name;
  }
  else
  {
    first_icon = NULL;
    first_filename = "";
  }
}

void Event_button::set_second_icon( const QString &second_icon_name )
{
  if ( !second_icon_name.isEmpty() )
  {
    delete second_icon;
    second_icon = new QIcon( second_icon_name );
    second_filename = second_icon_name;
  }
  else
  {
    second_icon = NULL;
    second_filename = "";
  }
}
/* Defination of class Tool_bar*/
Tool_bar::Tool_bar( QWidget *parent )
  : QWidget( parent )
{
  Event_button *channel = new Event_button( tr( "./pictures/channel_select_off.png" ),
                tr("./pictures/channel_select_on.png") );
  channel->setIconSize( QSize(70,25) );
  channel->set_size(150,125);
  Event_button *msg = new Event_button( tr("pictures/icon_msg_normal.png"),
            tr( "./pictures/icon_msg_onclick.png" ) );
  Event_button *chat = new Event_button( tr( "./pictures/icon_chat_normal.png" ),
            tr( "./pictures/icon_chat_onclick.png" ) );
  Event_button *full_screen = new Event_button(tr("./pictures/fullscreenicon_normal.png"),
                  tr( "./pictures/fullscreenicon_onclick.png" ));
  QHBoxLayout *tool_bar = new QHBoxLayout;
  tool_bar->addWidget(channel);
  tool_bar->addSpacing(20);
  tool_bar->addWidget(msg);
  tool_bar->addWidget(chat);
  tool_bar->addSpacing(20);
  tool_bar->addWidget(full_screen);

  setLayout(tool_bar);
  setMaximumWidth(240);
  setMaximumHeight(60);
  setMinimumWidth(240);
  setMinimumHeight(60);
  //resize(240,130);  
}
main.cpp:
#include <QApplication>
#include "channel_select_page.h"

int main(int argc,char *argv[])
{
  QApplication app(argc,argv);
  Ch_select_page a;
  a.show();
  return app.exec();  
}
[ 此贴被XChinux在2007-03-08 09:36重新编辑 ]
离线jy597329

只看该作者 1楼 发表于: 2007-03-07
把带Q_OBJECT宏的class类放到一个.h头文件里
快速回复
限100 字节
 
上一个 下一个