November 23, 2011

lowee lowee
Lab Rat
16 posts

[UI] Connection between QAction and SLOT

 

Hi everybody,

I’ve already made a Qt UI with Qt Designer and I got a menu bar with File menu (New File, Open File, Save File, Quit).

My wish is to connect the Quit action when its triggered to quit … I tried the command

  1. connect(Ui_MainWindow.actionQuit, SIGNAL(triggered()), this, SLOT(close()));

in the constructor of MainWindow but i got this error message :

  1. ..\mainwindow.cpp: In constructor 'MainWindow::MainWindow(QWidget*)':
  2. ..\mainwindow.cpp:10: error: expected primary-expression before '.' token

I don’t really understand this error …

So I tried with the UI connection action (“go to slot”) it works but i just want to know if it’s possible to make this connection and if it is possible how (because with the ui connection action, i will get too much functions…) ?

 Signature 

We are animals that our brain gives us the feeling that we aren’t

4 replies

November 23, 2011

Hostel Hostel
Hobby Entomologist
187 posts

Paste more code. What type is Ui_MainWindow? Where you declare actions? This could be a problem with include(missing include) or not. Hard to say without a context.

November 23, 2011

qxoz qxoz
Area 51 Engineer
602 posts

If you want create menu by yourself you must create an action and then bin them with Slot and menu item.

Something like that:

  1. void MainWindow::createActions()
  2. {
  3.     newAction = new QAction(tr("&New"), this);
  4.     newAction->setIcon(QIcon(":/images/new.png"));
  5.     newAction->setShortcut(tr("Ctrl+N"));
  6.     newAction->setStatusTip(tr("Create a new spreadsheet file"));
  7.     connect(newAction, SIGNAL(triggered()), this, SLOT(newFile()));

adding menu item

  1.     fileMenu = menuBar()->addMenu(tr("&File"));
  2.     fileMenu->addAction(newAction);

November 23, 2011

qxoz qxoz
Area 51 Engineer
602 posts

but if you want use already created Actions:

  1. connect(ui->actionName, SIGNAL(triggered()), this, SLOT(on_triggered()));

November 24, 2011

lowee lowee
Lab Rat
16 posts

Thanks a lot qxoz, I’ll create the entire interface including the menu item.

If I have some problems, I’ll post them in this post…

 Signature 

We are animals that our brain gives us the feeling that we aren’t

 
  ‹‹ Expected constructor, destructor, or type conversion before ‘(’ token      Translate UI file to .cpp and .h files ››

You must log in to post a reply. Not a member yet? Register here!