Create listening QLabel - c++

I am used to Java and new to QT/C++, and I have some problems creating a QLabel which changes text whenever a person is selecting a QListWidgetItem.
In my ui_GraphicsView.h I have setupUI() which makes the objects.
I try to make a layout its parent.
label = new QLabel(verticalLayout);
label->setObjectName(QString::fromUtf8("label"));
verticalLayout->addWidget(label);
In my .cpp-file I make use of connect in the constructor:
connect(list_widget, SIGNAL(itemSelectionChanged(), this, SLOT(updataDetails())));
updateDetails() is executed in my selectionChanged() method where it passes a string.
void GraphicsView::updateDetails(QString details){
label->setText(details);
}
This is resulting in the following error:
error: no matching function for call to ‘QLabel::QLabel(QVBoxLayout*&)
note: candidates are: QLabel::QLabel(const QLabel&)
note: QLabel::QLabel(const QString&, QWidget*, Qt::WindowFlags)
note: QLabel::QLabel(QWidget*, Qt::WindowFlags)
Everything worked out well before I made the adjustments described above. Any idea what is causing this error?

Read the error. QLabel::QLabel is constructor. Constructor expects pointer to widget, not to layout.
In fact, you want to do following:
label = new QLabel(parent_widget);
label->setLayout(layout)
EDIT: Strange, nobody noticed my error. Setting layout on label is strange. Better is
label=new QLabel();
layout->addWidget(label).
Label will get owner of layout as parent.

Related

QVariant conversion to QPainterPath

I have a problem right now with my mini-game I am making. The problem is as follows: I have created an level editor for my game and thus I had to create my own delegate and model, the problem occurs when I try to edit through a shapeeditor ( which more likely creates a painterpath ). I then return the painterpath through data but when I try to paint it with my delegate, qt tells me the following error:
/usr/include/qt4/QtCore/qmetatype.h:169: error: 'qt_metatype_id' is not a member of 'QMetaTypeId<QPainterPath>'
I am not quite sure why I am having this error. For information regarding the source code of the project, I can give if needed. But I am simply thinking the conversion from qvariant to qpainterpath isn't possible. They must be a way to do it.
Note: I tried to do the following
QVariant var = index.model()->data(index, Qt::DecorationRole);
QPainterPath path = var.value<QPainterPath>(); // The error occurs here, this is line 169
But this didn't work >.< Thanks if you can help me
Possible solution, is there anyway to create a pixmap from the painterpath? I could simply return the pixmap instead of the painterpath.
Looks like you need to use Q_DECLARE_METATYPE macro with QPainterPath
Like
Q_DECLARE_METATYPE (QPainterPath)
Here is documentation for the same.

Replace QWidget with a new QWidget

This questions to me reeks of maybe a lack of understanding of C++, as the possibilities I've considered for my problem all seem to make no sense on why this could be occuring. Feedback appreciated.
I'm using the form designer to create a form class with a table in it. I'm trying to replace the table with another table generated in a helper class. I'm only doing this so I can (hopefully) maintain the nice grid layout I've designed, and through pointer manipulation, get the replacement I desire. Here's some code snippets from the table form constructor and relevant calls :
//tableData is defined in the header file as a QTableWidget*
tableData = this->findChild<QTableWidget *>("tableData");
....
setup();
void setup(){
tableData = Utilities::createTable(this->file, tableDelim);
//createTable returns QTableWidget*
... other assignments, and label text updates, which seem to all work
}
My understanding is that tableData is a pointer, and if printed, will give the address of the QTableWidget from the layout. So then if I create a QTableWidget* and then assign tableData to that, tableData should now point to the new widget. Instead, I see only a blank screen.
I tried checking what the tableData pointer is before I assign it to the new QTableWidget*, and after. The second pointer shown is what is generated by createTable() :
QTableWidget(0x101272d40, name = "tableData") QTableWidget(0x10127b3b0, name = "test_sample2.nuc.stats")
QTableWidget(0x10127b3b0, name = "test_sample2.nuc.stats") QTableWidget(0x10127b3b0, name = "test_sample2.nuc.stats")
It seems the pointer is being reassigned, but the table drawn isn't the right one.
What gives?
My understanding is that you want to design the table layout in designer but fill in the data from an external source.
I would suggest, to just use the QTableWidget that is created in setupUi() and modify Utilities::createTable() such that it becomes Utilities::populateTable(QTableWidget & table, <all the other parameters you need>). (Or use QTableWidget * if you prefer - however I like putting the non-zero assertion responsibility on the caller...)
Apart from that, I agree with Sebastian Lange.
You are right with your assumption. You do set a variable to be a pointer to a object and next you set the variable to be a pointer to another object. You never change any objects, just your variable which is not used to display anything.
You would need to do something like:
//tableData is defined in the header file as a QTableWidget*
tableData = this->findChild<QTableWidget *>("tableData");
parentLayout = tableData->parent()->layout(); //Get the parent widget to add another table.
parentLayout->removeWidget(tableData);
delete tableData;
parentLayout->addWidget(createTable());
You need to use pTheContainerOfTheOriginalTableWidget->addWidget(tableData); See here: http://qt-project.org/forums/viewthread/16547
Be sure you remove the original tableWidget so you don't have two (I assume you don't want two).
If I understand you correctly we have such situation.
call of setupUi (which generated by qt tootls),
there there is something like this(pseudo code):
oldTablePtr = new QTableWidget(parent);
someLayout->addWidget(oldTablePtr);
So parent and layout hold value of oldTablePtr.
And if you set variable oldTablePtr nothing changed.
parent send QPaintEvent to oldTablePtr.
So you need call delete oldTablePtr, that remove this widget from list of childs of parent, and move newTablePtr to the same layout.
There's no need to replace it in code, you can do it in Qt Designer. Just place QTableWidget on form, then rightclick it and choose Promote widget in menu, then you will need just enter your classname.
Currently I don't have Qt Designer near me, so edits will be appreciated.

Demo program error: '' was not declared in this scope

I am new to Qt and have started with the demo program found here. I have renamed the class AddressBook to Dialog. It compiles and executes just fine. I want to add my own code to the example, so I add a new label as a private member of the class:
private:
QCheckBox *native;
QLabel *integerLabel;
QLabel *doubleLabel;
QLabel *itemLabel;
...
QLabel *questionLabel;
QLabel *warningLabel;
QLabel *errorLabel;
QLabel *newLabel; // <== This is new
QErrorMessage *errorMessageDialog;
In the constructor Dialog::Dialog I (try to) allocate memory to the variables:
warningLabel = new QLabel;
warningLabel->setFrameStyle(frameStyle);
QPushButton *warningButton = new QPushButton(tr("QMessageBox::&warning()"));
errorLabel = new QLabel;
errorLabel->setFrameStyle(frameStyle);
QPushButton *errorButton =
new QPushButton(tr("QErrorMessage::showM&essage()"));
newLabel = new QLabel; // <== Error
newLabel->setFrameStyle(frameStyle);
As far as I can tell, I have created and allocated the variable correctly in this class, by copy and pasting *errorLabel and changing its name. Yet the compiler issues the error:
error: 'newLabel' was not declared in this scope
When I click the error, it takes me to the "new" line in the constructor. If I remark out the two lines in the constructor referring to newLabel, the program compiles. It IS in this scope in my mind. What mistake am I making here? How do we prevent these type issues in the future? (I have searched tens of similar posts and was able to identify the OP's mistake in 30s or less, but I cannot find my own, nor can I find a post where variables are declared in the class, but not found in the constructor.)
Thanks in advance,
Kyle
I have solved the problem. Even though it is not the answer I expected, I post this answer just in case it is helpful to the rest of the community.
I created a new project and copied the cpp/h files from the non-working project into the new project. The new project compiles. With the new project I can now add other private variables to the class and access them in the constructor (as well as the IDE). All parameters I have easy access to show the projects to be identical, yet one compiles and the other does not. I can only conclude the project itself somehow became corrupted (which does not inspire confidence).
While I am relieved it was not a stupid programmatic mistake on my part, I am disappointed in not being able to find and fix whatever was wrong with the project files. Thanks to all those who offered suggestions.

cant get a pointer to wxwidget object, using wxsmith

straight to the point:
Im learning wxsmith and wxwidgets toolkit, i created some basic GUI containing one button and 2 static text fields. GUI is compilling ok so far. My frame name is proba2Frame, then im adding my own function which is not a member of any class but i declared in header file for proba2Frame that my function is a friend. Below is code of my function:
wxStaticText * dawajpointera()
{
wxStaticText * text;
text = proba2Frame.wxStaticText.StaticText1;
return text;
}
im getting error:
expected primary-expression before ‘.’ token
What exactly im doing wrong and how to get a pointer StaticText in case my solution is completely wrong ?
Thank You in advance
You make it sound like proba2Frame is the name of a class inheriting wxFrame?
If so, you're haveing problems because you haven't created an instance of proba2Frame, and you're trying to access a part of it that hasn't been constructed. Your main frame class is simply a template for your GUI, not the GUI itself.
The best way to go about it would probably be to take an instance of proba2Frame as a parameter-
wxStaticText* dawajpointera(proba2Frame *frame)
{
return frame->StaticText1;
}
Of course, that function itself was a bit pointless, but I'll assume that you're going to do something more involved with the pointer afterwards, and want it set to a pointer named text within the function for the sake of brevity.
void func(proba2Frame *frame)
{
wxStaticText *text = frame->StaticText1;
// Do something with text
}
If you're doing this, though, please consider making the function a method of proba2Frame.
wxStaticText is the name of a wxWidgets class. You should not be naming attributes of your frame 'wxStaticText'. Despite the code you have posted, I doubt that you have really done such a terrible thing. What you probably meant to write, I would guess, is:
text = proba2Frame.StaticText1;
I am guessing that the name of the attribute is StaticText1, a pointer to an instance of the wxStaticText class.

How to add a widget (QPushButton for example) dynamically to a layout built in designer

I'm having a play around with Qt mainly looking to rewrite an old java app for symbian and I have got my self a bit confused.
I should first of all explain that C++ is not my kung-fu, and that may be the cause of the problem.
What I am trying to do is add a simple QPushButton to a Vertical Layout in a main window which has been built in qt designer at run time.
My example code is something like this...
QPushButton button = new QPushButton();
QString text("Testing Buttons");
button.setText(text);
//How do we add children to this widget??
ui->myLayout->addWidget(button);
The errors I am getting are as follows...
/home/graham/myFirstApp/mainwindow.cpp:22:
error: conversion from ‘QPushButton*’
to non-scalar type ‘QPushButton’
requested
/home/graham/myFirstApp/mainwindow.cpp:27:
error: no matching function for call
to
‘QVBoxLayout::addWidget(QPushButton&)’
/home/graham/myFirstApp/../qtsdk-2010.05/qt/include/QtGui/qboxlayout.h:85: candidates are: void
QBoxLayout::addWidget(QWidget*, int,
Qt::Alignment)
Now I know the first error has something to do with pointers but I don't know what, if anyone is able to clear up my confusion and provide example code that would be great.
Regards
Graham.
This is a merely C++ problem, you need to use asterisk to declare the button as pointer when you use new-operator.
QPushButton* button = new QPushButton();
button->setText(text);
ui->myLayout->addWidget(button);
QPushButton button = new QPushButton();
A pointer to QPushButton is not a QPushButton. That's what your compiler's bitching about and that's your problem.