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.
Related
so I'm not sure if this is a bug in Qt or if I just don't understand something, but i have this class:
class myClass : public QDialog, private Ui::myClass
{
Q_OBJECT
public:
explicit myClass(QWidget *parent = 0);
private:
QList<ushort> list1;
QList<ushort> list2;
}
I create this window by clicking on an action in another window:
void mainWindow::on_myClassAction_triggered()
{
myClass *mc = new mc(NULL);
mc->show();
}
So now things get weird. Even if i don't use list1 and list2 anywhere in myClass, the program will crash when i close or sometimes open myClass. If i comment out one, or both of them, it works. What is going on here??
I'm using Qt Creator. I just re-ran QMake and now it works. Definitely was some sort of bug within Qt/Qt Creator.
This is not a bug of any sort. It's a classic binary incompatibility problem: you've had some code that depended on the layout of some of your classes, but the outdated Makefiles did not capture that dependency. Thus when you changed the layout of the class, the dependent code didn't get recompiled. It would be way too expensive for qmake to rescan the entire project looking for such dependencies every time you build it. It's on you to re-run qmake when you change the code to introduce such binary incompatibilities.
For small projects, you may simply wish to always rebuild the code, forcing a qmake re-run.
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.
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.
I am using Qt 4.7.2 under Fedora 14.
I have a working library of custom widgets which I am trying to integrate into Qt Designer.
There is a simple widget base class, then some more complex widgets inheriting from it.
The simple widget integrates fine, but I have a problem with the more complex ones.
The problem comes from the fact that that the more complex ones require a special structure to be passed to them on creation. So my code for creating a complex widget in the plugin is:
QWidget *myPlugin::createWidget(QWidget *parent)
{
my_struct *xyz = new my_struct;
return new myWidget("MyWidget",xyz, parent);
}
my_struct is just a simple C style struct with ints, doubles and arrays.
Then when I start Qt Designer with this plugin I get:
/usr/lib64/qt4/bin/designer: symbol lookup error: /usr/lib64/qt4/plugins/designer/libMyPlugin.so: undefined symbol: _ZN8MyWidgetC1E7QStringP17my_structP7QWidget
I am building a release version of the plugin, not a debug version.
I also tried defining xyz as a static class variable in the plugin and just passing its address to the constructor, but I still get the same error.
I also tried adding xyx as an unused static class variable to my simple widget and that does not give the error, so the error seems to be coming from the call to the myWidget constuctor.
Can anyone help me resolve this?
Thanks Arne,
You were right.
I added my library explicitly to the plugin's .pro file and then it worked.
What puzzles me is how could I build the plugin with no errors without the library?
And why did my simple widget work without the library?
The error message looks as if the linker did not find a definition for the constructor myWidget(QString, my_struct*, QWidget*). Have you added the relevant source file to your release build? Do you actually have a definition for this constructor? It is also possible that you have to exchange the object file linking order. I have seen this with larger projects in the past, so I wouldn't be too surprised. The object file containing the definition needs to be stated before the one using the definition.
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.