why console disapper after enter the price in this code? - console

#include<iostream.h>
class car
{
float price;
public:
void a_price()
{
cout<<"Price :";
cin>>price;
}
};
void main()
{
car ford;
ford.a_price;
}
it will get the entry of price from user and then disappears the console
and if i write getch() then it will stay why we have to write that ? this is the concept of c language. and if i write
int main ()
{
block of code
return 0;
}
then also console is disappears.
and if am writing below code then console stays perfect:
#include<iostream.h>
#include<conio.h>
class car
{
float price;
public:
void a_price()
{
cout<<"Price :";
cin>>price;
}
};
void main()
{
car ford;
ford.a_price;
getch();
}
and to clear screen why we have to use
system("cls");
when we have
clrscr();

When you run your program from GUI it creates the console window, runs, and when it finishes the console window is closed automatically. There are many ways of preventing this depending on your OS, IDE ...
getch() at the end is expecting input, so it prevents console from closing (not in all cases, for more details see this answer).
Instead of trying to prevent closing you can run your program from terminal (command line - CLI). If you are new to programming you should learn some terminal basics.
If you are using Windows:
<Windows key + r> will open the 'run' dialog box. Type 'cmd' and press enter.
With your console open change current directory to your program directory:
cd /d "path to directory your program is in"
you can view the content of the directory with dir command, and run your program by typing it's name. Use <TAB> for autocomplete in console window.

Related

Why does starting a QProcess freeze the GUI in a QWidget subclass but not in a QLineEdit subclass?

I have a simple executable file (english.exe) that changes Windows' input language to English. I want to run this executable when the user interacts with various components of the GUI.
If I run the executable in the focusInEvent of QLineEdit, I get the expected behavior: the program runs, and there is no lag.
void MyLineEdit::focusInEvent(QFocusEvent *e)
{
QLineEdit::focusInEvent(e);
QProcess * switchInput = new QProcess(this);
switchInput->start( "c:/english.exe");
}
I want to run analogous code when a particular widget is activated, for which see this answer. (In fact, I would like to use focusInEvent for a QWebEngineView, but this resolved bug report notwithstanding, that event is never called; I am using Qt 5.12.6.) But with the following code, the GUI hangs.
bool MyWidget::event(QEvent *event)
{
bool result = QWidget::event(event);
if( event->type() == QEvent::WindowActivate )
{
// qDebug() << "QEvent::WindowActivate";
QProcess * switchInput = new QProcess(this);
switchInput->start( "c:/english.exe" );
}
return result;
}
Using the qDebug() code, I'm only getting one "QEvent::WindowActivate" message.
What is the difference between the two calls? Is there an alternative solution?
Edit: Whatever solution I find may not be of general interest. The program I'm running is a command-line AutoHotKey script (with contents PostMessage, 0x50, 0, 0x0409,, A) (no output or user interaction). For comparison I tried running ipconfig.exe (to take a command line program at random) and the problem goes away. So there must be something about the AutoHotKey script, or about AutoHotKey itself.

QSettings persistence constantly writing to config file on initial startup

So I have a qt app running on Linux. When the app is initially launched, QSettings is constantly writing to the settings.conf file even though no changes were made to QSettings. Once a user changes any setting, it stops writing and acts like normal and only writes during changes. Our hardware does not have a power button and so never shuts off and is constantly plugged in so having QSettings constantly writing to the .conf file on start up is a problem.
I looked for timers to make sure no timer was prompting to write and there are none. Tried settings.sync(). I even set a throwaway value on start up, since after changing a value in the app after initial launch it stops writing, but that didn't work. All settings persistence is written this way and after I comment out the settings.setValue at the bottom of the code, the .conf write process works as supposed to. I have no idea why it is writing when values are set from a power button click. Thoughts?
Here is the code and flow:
void MainWindow::onNavBarButtonClicked(int buttonClickedIdAsInt)
{
//.....
case NavBarButton::POWER:
activeScreenContainer->setCurrentWidget(userScreen);
activeScreenContainer ->raise();
navBarFrame->raise();
navBarActiveContainerSeparator->hide();
logoUserScreen->show();
logoUserScreen->raise();
if(orientationSelected == appPersistence::PORTRAIT_ORIENTATION_VALUE) {
timeAndUserFrame->hide();
}
emit userLoggedOut();
}
to
connect(this, &MainWindow::userLoggedOut, musicScreenModel,
&MusicScreenModel::onUserLoggedOut);
to
void MusicScreenModel::onUserLoggedOut()
{
emit userLoggedOutTreble(currentTrebleValue);
}
to
connect(musicScreenModel, &MusicScreenModel::userLoggedOutTreble,
settingsScreenModel, &SettingsScreenModel::onUserLoggedOutTreble);
to
void SettingsScreenModel::onUserLoggedOutTreble(int trebleToStore)
{
settings.setValue(appPersistence::MUSIC_TREBLE_KEY + loggedInUser,
trebleToStore);
}
And main:
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QCoreApplication::setOrganizationName("Organization");
QCoreApplication::setApplicationName("AppName");
QSettings::setPath(QSettings::Format::NativeFormat,QSettings::Scope::UserScope, "/opt/");
QSettings settings;
int fontFamilyId = QFontDatabase::addApplicationFont(":/fonts/Quicksand-Bold.ttf");
settings.setValue(fontStyle::QUICKSAND_BOLD_FAMILY_ID, fontFamilyId);
if(!settings.value(appPersistence::ORIENTATION_SELECTED_KEY).isValid()) {
settings.setValue(appPersistence::ORIENTATION_SELECTED_KEY, appPersistence::LANDSCAPE_ORIENTATION_VALUE);
}
if(true) {
DevOnlyFunctions::seedRng();
}
testBuild::setIsTestBuild(false);
MainWindow w;
w.show();
return a.exec();
}
So I finally figured out what the was going wrong. We have a timer that shows whether certain Can Bus devices are present. If they are not present we hide the icons and log out the user directly through calling the onUserLoggedOut() method and not through a connect statement. I was constantly searching for a timer and connect statement and not a direct method call nested within a switch that uses a timer.

JavaFX - Why system.out.println doesn't work in my application

I want to get a console output in my application debugging, but wherever I put println method console doesn't print anything. For example, I want to get "Hi" after clicking the "play" button, everything except println, will invoke normally.
#FXML
protected void handlePlayButton(ActionEvent event) {
System.out.println("hi!");
filePath = cTable.getSelectionModel().getSelectedItem().getFilePath();
index = cTable.getSelectionModel().getSelectedIndex();
player = new Play();
player.player(filePath);
}
There might be an issue with where the stdout is pointing. Are you running it inside an IDE or might it have been redirected somewhere else in your code?

Qt embedded screen rotation inside app

In our target device, we run our QtE app with -qws argument. To rotate the screen, we specify "-display transformed:rot90" as the app argument and it works well.
However, we have a feature to rotate screen inside the app, so we try below API documented in QScreen:
QWSDisplay::setTransformation(QTransformedScreen::Rot90, 0);
But this API doesn't work at all. It's no error message in the console output.
Does anyone know what's going on about this API? Do we need to enable something else?
Contrary to other qt documentation, documentation for the embedded part of qt is indeed poor. After few days of fiddling with it, I finally managed to solve it.
First thing to do is to compile the library with -qt-gfx-transformed option (along with whatever you need).
Then you compile your application, and start it with the option you already used to activate the transformation driver. I actually started like this :
export QWS_DISPLAY=Transformed:Rot90:0
./app
As a test, I implemented this, to test whether the rotation works :
class X : public QObject
{
Q_OBJECT
public :
X() :
QObject()
{
QTimer *t = new QTimer( this );
connect( t, SIGNAL(timeout()), this, SLOT(OnTimerEvent()));
t->start( 2500 );
}
public slots :
inline void OnTimerEvent()
{
static int v = 0;
++v;
QWSDisplay::setTransformation(v%4);
std::cout<<v<<std::endl;
}
};
So, in the timer slot, I am changing the orientation with QWSDisplay::setTransformation function.

How can I create a button on a form in a J2ME application?

I attempt to make a simple "hello world" application, where on clicking the button , it prints a string "hello world". How can I add a button on a form?
I need to create a button, on which when I click it can produce a string. How can I add a button without using canvas in j2me?
There is an API for this, but you better think twice whether you really need it.
API is desctibed in the Appearance modes section for lcdui Item objects
The StringItem and ImageItem classes have an appearance mode attribute that can be set in their constructors. This attribute can have one of the values PLAIN, HYPERLINK, or BUTTON. An appearance mode of PLAIN is typically used for non-interactive display of textual or graphical material. The appearance mode values do not have any side effects on the interactivity of the item. In order to be interactive, the item must have one or more Commands (preferably with a default command assigned), and it must have a CommandListener that receives notification of Command invocations...
A StringItem or ImageItem in BUTTON mode can be used to create a button-based user interface...
Note that this section also explains cases when using button appearance might be problematic:
...This can easily lead to applications that are inconvenient to use. For example, in a traversal-based system, users must navigate to a button before they can invoke any commands on it. If buttons are spread across a long Form, users may be required to perform a considerable amount of navigation in order to discover all the available commands. Furthermore, invoking a command from a button at the other end of the Form can be quite cumbersome. Traversal-based systems often provide a means of invoking commands from anywhere (such as from a menu), without the need to traverse to a particular item. Instead of adding a command to a button and placing that button into a Form, it would often be more appropriate and convenient for users if that command were added directly to the Form. Buttons should be used only in cases where direct user interaction with the item's string or image contents is essential to the user's understanding of the commands that can be invoked from that item.
From the class diagram I found in an old J2ME book, and which is online at http://www.stardeveloper.com/articles/display.html?article=2002121101&page=2 it seems that J2ME don't do buttons. Well no need for them on an old mobile phone.
Just create a "hello" command and add it to a menu or form. The system will then put it on whatever button is available on your device. For touch screen devices that probably turns it into something clickable.
Here's the code
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.TextBox;
import javax.microedition.lcdui.TextField;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
public class HelloWorld extends MIDlet implements CommandListener {
private static final String HELLO_WORLD = "Hello, World!!";
private Form form= new Form ("");
private Command exit= new Command("Exit", Command.EXIT, 0x01);
private Command ok= new Command("OK", Command.OK, 0x01);
private Command hello= new Command("HELLO", Command.SCREEN, 0x01);
private TextBox textBox= new TextBox("Hello World", HELLO_WORLD, HELLO_WORLD.length(), TextField.UNEDITABLE);
public HelloWorld() {
this.form.addCommand(exit);
this.form.addCommand(hello);
this.form.setCommandListener(this);
this.textBox.addCommand(ok);
this.textBox.addCommand(exit);
this.textBox.setCommandListener(this);
}
protected void destroyApp(boolean unconditional)
throws MIDletStateChangeException { }
protected void pauseApp() { }
protected void startApp() throws MIDletStateChangeException {
Display.getDisplay(this).setCurrent(this.form);
}
public void commandAction(Command c, Displayable d) {
if (c == this.exit) {
this.notifyDestroyed();
}
if(c == this.ok) {
Display.getDisplay(this).setCurrent(this.form);
}
if(c == this.hello) {
Display.getDisplay(this).setCurrent(this.textBox);
}
}
}

Resources