You are not logged in.

Dear visitor, welcome to QtForum.org. If this is your first visit here, please read the Help. It explains in detail how this page works. To use all features of this page, you should consider registering. Please use the registration form, to register here or read more information about the registration process. If you are already registered, please login here.

1

Friday, December 30th 2011, 1:04pm

how to add a function that uses qt element inside int

want to add functions inside int in qt main.cpp filethat uses qt functions and lirbries
for eg. a samplke piece of code

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <QtGui/QApplication>
#include "mainwindow.h"
#include "QTextBrowser"
#include <QTextEdit>
#include <QPushButton>
#include <QWidget>
#include <QHBoxLayout>
int main(int argc, char *argv[])
{
	QApplication a(argc , argv);
	QWidget * win = new QWidget();
	QHBoxLayout * layout = new QHBoxLayout(win);
	QTextEdit * text = new QTextEdit();
	text->setText("hi are you all");
	QPushButton * button = new QPushButton();
	QTextBrowser * result = new QTextBrowser();
	result->setText("hi are you all");
	layout->addWidget(text);
	layout->addWidget(button);
	layout->addWidget(result);
	win->resize(500 , 500);
	win->show();
	return a.exec();
}

i void like to add a function here which uses qt variables used in main inside int because otu side int i cant vraible like text that i used in int main
i hope you got what i mean

2

Friday, December 30th 2011, 4:05pm

what on earth do you mean by 'inside int'? int is a primitive type.

int x = 5;

to pass variables around, you do it the same as in any other c++ program.

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
// #includes
#include <iostream>

void printWidgetText(QTextEdit* txt)
{
  std::cout << txt->text().toStdString() << std::endl
}

int main(int argc, char *argv[])
{
 	QApplication a(argc , argv);
	QWidget * win = new QWidget();
	QHBoxLayout * layout = new QHBoxLayout(win);
	QTextEdit * text = new QTextEdit();
	text->setText("hi are you all");
	QPushButton * button = new QPushButton();
	QTextBrowser * result = new QTextBrowser();
	result->setText("hi are you all");
	layout->addWidget(text);
	layout->addWidget(button);
	layout->addWidget(result);
	win->resize(500 , 500);
	win->show();

        printWidgetText(text);
        printWidgetText(result); // QTextBrowser  inherits qtextedit so we can do this
        
	return a.exec();
}
If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

This post has been edited 1 times, last edit by "Amleto" (Dec 30th 2011, 4:11pm)


3

Saturday, December 31st 2011, 4:35am

thnaks

thnaks man

This post has been edited 2 times, last edit by "ubuntu" (Dec 31st 2011, 9:36am)