LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-05-2005, 06:57 AM   #1
Gnarg
Member
 
Registered: Feb 2004
Location: Canada
Distribution: Gentoo Linux
Posts: 44

Rep: Reputation: 15
Problem with Slots and Signals


Hi all,

I have read many tutorials on programming in KDE with QTDesigner. When I write small apps to learn, everything compiles and runs fine including any slots and signals I have. I wanted to learn a little more and understand more how slots and signals work so I decided to write a small learning app not using QTDeisgner and do it all manual.

My problem is I am writing an app with 2 buttons one with a Quit button and another with an Open button that calls a function openmyimage. From what I have been reading it looks like its fine but when I compile ( no warnings or errors ) and run the small learning app I keep getting a message in the Application tab in KDevelop:

QObject::connect: No such slot QApplication::openmyimage()
QObject::connect: (sender name: 'openbutton')
QObject::connect: (receiver name: 'chtest')

Then the application shows with two buttons.

This causes the Open button to do nothing when clicked. Right now all I wanted to do in the openmyimage function is to call a qwarning to display a message in the Application tab.

I need another pair of eyes to pinpoint my mistake. Can anyone please take a look at my code and let me know where I went wrong. Since this is just a learning app for me to learn Signals and Slots a little better I did everything in one file and compiled so copying this code and pasting it in your dev environment should work.


Code:
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <qapplication.h>
#include <qfont.h>
#include <qpushbutton.h>




class MyWidget : public QWidget
{
	//Q_OBJECT
public:

	MyWidget(QWidget *parent=0, const char *name=0);
	

public slots:
	void openmyimage();	

signals:
	void clicked();
};



MyWidget::MyWidget (QWidget *parent, const char *name) : QWidget(parent, name) 
{

	setMinimumSize (200,120);
	setMaximumSize (200,120);
  
  QPushButton *quit = new QPushButton("Quit", this, "quit");
  quit->setGeometry(10,10,75,30);
  quit->setFont( QFont("Times", 18, QFont::Bold));

  QPushButton *btnopen = new QPushButton("Open1", this, "openbutton");
  btnopen->setGeometry(10,45,75,30);
  btnopen->setFont( QFont("Times", 18, QFont::Bold));

  
   
  connect (quit,    SIGNAL(clicked()), qApp, SLOT(quit()) );
  connect (btnopen, SIGNAL(clicked()), qApp, SLOT(openmyimage()) );
  
  qWarning("Finished in Constructor");
}




void MyWidget::openmyimage()
{
 qWarning("test");
}






int main(int argc, char *argv[])
{
  QApplication a(argc, argv);

  MyWidget w;
  
  
  
  w.setGeometry (100,100,200,120);
  a.setMainWidget( &w);
  w.show();
  return a.exec();
}

I have alrady tried moving each class into its own .cpp and .h file with the same results. Changed the slot name,
Changed

Code:
connect (btnopen, SIGNAL(clicked()), qApp, SLOT(openmyimage()) );
to

Code:
connect (btnopen, SIGNAL(clicked()), this, SLOT(openmyimage()) );

When I add Q_OBJECT into the MyWidget class I get a compile error with just an exit status 2.

Thanks for taking the time to look.


Gnarg
 
Old 10-06-2005, 03:15 PM   #2
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Uncomment the "Q_OBJECT" and try this:
http://www.qtforum.org/thread.php?postid=1329

'Hope that helps .. PSM
 
Old 10-07-2005, 07:19 AM   #3
Gnarg
Member
 
Registered: Feb 2004
Location: Canada
Distribution: Gentoo Linux
Posts: 44

Original Poster
Rep: Reputation: 15
Hi paulsm4,

Thanks for your reply, I have removed the Q_OBJECT macro before and allows me to compile successfully but then I run into the whole connect issue I mentioned. I did find an alternative which works for me so I'll use that and learn from it.

All I did was create a QMAKE Application and removed all the code that was not needed like a toolbar, menu and status bar. What is left is a small framework with basically nothing. Then I added my buttons and connected the buttons to some slots and as I click on the buttons I get my qWarning as I expected.


There is a difference from the code I posted and what was created from the framework is my code uses QWidget where the framework used QMainWindow.


If you or anyone else knows what I did wrong I still would like to see where I went wrong.





Gnarg
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
No pc-card slots found Ephracis Linux - Laptop and Netbook 2 03-24-2005 09:59 PM
how to find out if im using 1 or 2 slots for ram? jfall Linux - Hardware 8 03-07-2005 07:44 PM
Motherboard with PCI-E slots satimis Linux - Hardware 0 07-30-2004 03:41 AM
2 different signals to soundcard problem citan Linux - Newbie 1 05-20-2004 02:56 AM
PCI slots dunkyb Linux - Hardware 5 07-20-2003 08:20 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:40 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration