Save spinbox pointers to the list defined in MainWindow class (QList<QSpinBox*>).
Save spinbox pointers to the list defined in MainWindow class (QList<QSpinBox*>).
Thanks for the quick reply. When I do that I get the following error:This is what I did:/mainwindow.cpp:68: error: 'spinbox' cannot appear in a constant-expression
It should be QList<QSpinBox*>, however defining the list as a local variable will not do you any good. You need to be able to access that variable from scopes other than this function.
This is really no place to teach you C++. The issue you have is completely unrelated to Qt. You can make the variable a class member or a global one but I'm afraid that without proper C++ skills you'll get stuck again. I suggest you read a good book on C++ or do a couple of C++ tutorials.
I'm really new to c++ actually, so im not use to classes like you have in java also. I'm pretty good with just ordinary c though and in C to declare something to be accessed from anywhere you declare it at the top of your program. Then it may be accessed by "functions" rather than classes. It's good that it's not qt related so I just need to find out how to declare stuff globally in c++.
But can you tell me how to use functions in qt? I wanted to make the whole creating layout part a function.
Qt is exactly the same as regular C++. Once you learn C++, you'll know how to use functions in Qt as well. Using functions in C++ is exactly the same as in C, by the way.
Well actually, I can use functions in the main.cpp, I just need to know how to use them in mainwindow class please.
I already said, functions in C and C++ work the same way, with respect to scopes (such as classes).
Yes I understand, I can use it in c++ in the main.cpp, what I want to know is how to use in mainwindow.cpp. I put in the exact same code and it says "expected a declaration" when its supposed to be a function...
if you create a mew qt gui file it should do that for you automatically other wise you can do it manually->
use a header like this:
Qt Code:
#ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> { Q_OBJECT public: MainWindow(); ~MainWindow(); private slots: void functionSlot(); private: void function(); //QWidgetType *QWidgetName..... //ex: QPushButton *pushButton; }; #endifTo copy to clipboard, switch view to plain text mode
then on the mainWindow cpp file
and on the main.cpp fileQt Code:
#include <QtGui> #include "mainwindow.h" MainWindow::MainWindow(){ pushButton = newQPushButton; function(); } void MainWindow::function(){ pusButton->setText("Button"); //do something }To copy to clipboard, switch view to plain text mode
Qt Code:
#include <QtGui/QApplication> #inlucde "mainwindow.h" int main(int argc, char *argv[]){ MainWindow w; w.show(); return a.exec(); }To copy to clipboard, switch view to plain text mode
Cyrebo (30th March 2013)
Thanks man, I've been looking for something like that all day but I still get this error on runtime..
-build-desktop-Qt_4_8_3_in_PATH__System__Release/moc_mainwindow.cpp:-1: error: undefined reference to `MainWindow::dbinfoSlot()'
because you need to put void before MainWindow more than likely.
like this:
Qt Code:
void MainWindow::somethingSlotIForgotName(){ //do something }To copy to clipboard, switch view to plain text mode
Edit:: if it is a private/slot you have to declare it as void/unsigned/int/ect
if it is the public function (the main one for the class) you do not have to declare it.
It was the name dbInfoSlot(), it was unused..I took that line out of the private slots and it worked but the program runs the function twice or something because it creates two instances of...I'll just show you
Qt Code:
void MainWindow::dbinfo() { int value; QSqlQuery qry; if (qry.exec("SELECT name FROM customers")) { while(qry.next()) { qDebug() << qry.value(0).toString(); if(qry.isValid()) { label->setGeometry(0,0,80,41); ui->verticalLayout->addWidget(label); ui->verticalLayout->addWidget(spinbox); value = spinbox->value(); qDebug() << value; } } } else { qDebug() << qry.lastError(); } }To copy to clipboard, switch view to plain text mode
I only want to create a label and spinbox for each customer but when I enter only 1 customer it creates two at run time...
Bookmarks