Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Native Development

Reply
Trusted Contributor
Posts: 160
Registered: ‎09-13-2012
My Device: 9900
My Carrier: vodafone

Qt

Hi all,

 

I cannot get past adding my class that extends QWidget to a QVBoxLayout in my c++ project, sounds so simple! I just want to be able to build a top part of my screen (title bar) in a different class to MyWindow.cpp to keeps things clean.

 

I get a within this context compiler error in the MyTitle.h if I add (: public QWidget) and if i remove it then it won't be added to a layout (addWidget(MyTitle) not recognised error) so it seems i need to declare that it extends QWidget in the .h as well as in the .cpp file.

 

I will post the code, if you can help me to understand how to do this it would be so helpful as I have tried to learn this a few times now. I have read the tutorials and my code seems to match up.

 

/*
 * TitleBar.h
 *
 *  Created on: Oct 12, 2012
 *      Author: Chris
 */
#include <qwidget>
#ifndef TITLEBAR_H_
#define TITLEBAR_H_

namespace std2 {
}

class TitleBar  {
	Q_OBJECT
public:
	TitleBar( );
	virtual ~TitleBar();
};


#endif

 

 

#include "TitleBar.h"
#include <QtGui>
#include <QMouseEvent>
namespace std2 {
};
class Titlebar
{
	private:
		QWidget *parent;
		QPoint cursor;

	public:
		Titlebar( QWidget *parent )
		{
			QLabel *label = new QLabel( parent->windowTitle() );
			QPushButton *buttonClose = new QPushButton( "x" );

			QHBoxLayout *layout = new QHBoxLayout( parent );
				layout->addWidget( label, 1 );
				layout->addWidget( buttonClose );

			//connect( buttonClose, SIGNAL( clicked() ), parent, SLOT( close() ) );
		}

 

 

 

Developer
Posts: 195
Registered: ‎05-09-2012
My Device: Playbook, Z10 LE

Re: Qt

[ Edited ]
Contributor
Posts: 41
Registered: ‎09-14-2012
My Device: Playbook 64Gb
My Carrier: n/a

Re: Qt

I'm not sure why you need to declare you titlebar class twice -- once on the .h file and once in the .cpp file. I would have expected to see the .h file much as you have it, then in the .cpp file the definition of the constructor would be of the form

 

TitleBar::TitleBar (QWidget *parent)

  {

  // etc

  }

 

That is, without the surrounding class { } construct.

 

I suspect your compiler error arises because when you add the inheritance from QWidget, you end up with two conflicting defintiions of the same class.

 

C++ does allow implementation and declaration to be mixed in the same file, but my experience is that code is more manageable if you put all the class {} constructs in header files, with no actual implementation at all, and all the implementation in .cpp files, with no declaration at all. That's just my way, of course; other people have different views.

 

 

Trusted Contributor
Posts: 160
Registered: ‎09-13-2012
My Device: 9900
My Carrier: vodafone

Re: Qt

Thanks kboone, you are right, mixing declearation and implementation in .h file was the issue. I have seperated and it builds fine now, I just cant see the titlebar but I haven't spent too much time on that.

 

Zingham, thanks for the links - they seem very suitable for what im trying to learn about so its bookmarked.

 

C

Developer
Posts: 188
Registered: ‎01-27-2012
My Device: playbook
My Carrier: ...

Re: Qt

Perhaps titleBar->show();

Is what your looking for.

Cheers,

Jon